如何将datagridview中删除的数据更新到数据库

如题所述

下面是删除指定的行并更新到库(实际上原理很简单,就是从数据库中删除选定的行,然后清除dataGridView中的所有数据再执行一次查询就行了):

1
2
3
4
5
6
7
8
9
10
11
12
13
14

private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
string temp = "delete from qty where PartNum='" + dataGridView1["PartNum",indexrow].Value.ToString() + "'";
if (MSSQL.ExecuteQuery(temp))
{
AddControls a = new AddControls();
a.DeleteDataGridView(dataGridView1);
a.SetDataGridViewData(dataGridView1, str, sqldrr, ddrr);
}
}
catch { }
}

里面MSSQL.ExecuteQuery是这样定义的:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

/// <summary>
/// 执行sql语句
/// </summary>
/// <param name="cmdString">查询,更新,删除语句</param>
/// <returns>成功返回true,失败返回false</returns>
public static bool ExecuteQuery(string cmdString)
{
MsSql();
SqlConnection conn = new SqlConnection(connectString);
SqlCommand cmd = new SqlCommand(cmdString, conn);
cmd.CommandText = cmdString;
try
{
conn.Open();
if (cmd.ExecuteNonQuery() > 0)
{
conn.Close();
return true;
}
else
{
return false;
}
}
catch (SqlException Sqlex)
{
Console.WriteLine(Sqlex.Message);
return false;
}
}
温馨提示:答案为网友推荐,仅供参考
相似回答
大家正在搜