c#在datagridview中删除指定行并更新到数据库 100
2个回答
展开全部
下面是删除指定的行并更新到库(实际上原理很简单,就是从数据库中删除选定的行,然后清除dataGridView中的所有数据再执行一次查询就行了):
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是这样定义的:
/// <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;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询