我有一个用于编辑表格的 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( 数据表. GetCchanges ())
根据修改后的栏目, 智能生成了最起码的设定条款 。
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
的存储程序?