我有一个用于编辑表格的 DataGridView。以下代码用于更新表。
using (SqlConnection con = new SqlConnection("...."))
{
con.Open();
SqlDataAdapter da = new SqlDataAdapter("select * from T", con);
SqlCommandBuilder cb = new SqlCommandBuilder(da);
cb.ConflictOption = ConflictOption.OverwriteChanges;
da.UpdateCommand = cb.GetUpdateCommand();
// da.UpdateCommand.CommandText = "exec sp1 @p1, @p2, @p3...";
da.InsertCommand = cb.GetInsertCommand();
da.DeleteCommand = cb.GetDeleteCommand();
da.Update(datatable.GetChanges());
}
我发现da.Update(datatable.GetChanges())
根据修改后的列巧妙地生成了最小集合子句。
update T set c1 = @p1 where K = @p2 -- If only c1 is changed in the grid
update T set c1 = @p1, c2 = @p2 where K = @p3 -- if both c1 and c2 is changed
update T set c4 = @p1 where K = @p2 -- if only c4 is changed
......
如何编写存储过程CommandText
?