C# SqlCommandBuilder.SetAllValues不再工作
我为我的C# SqlCommandBuilder.SetAllValues不再工作,c#,winforms,sqlcommandbuilder,C#,Winforms,Sqlcommandbuilder,我为我的SqlCommandbuilder.SetAllValues=false设置了值 几年前,当我实现它时,我使用profiler进行了检查,它工作正常,只有更改的字段出现在由SqlCommandBuilder生成的update命令中 但现在我发现它不再工作了。 调试时,我注意到就在调用adapter.Update(table)之前,DataTable中的列在DataRowVersion.Original和DataRowVersion.Current 我不知道从哪里开始寻找这个,所以我希望有
SqlCommandbuilder.SetAllValues=false设置了值代码>
几年前,当我实现它时,我使用profiler进行了检查,它工作正常,只有更改的字段出现在由SqlCommandBuilder
生成的update命令中
但现在我发现它不再工作了。
调试时,我注意到就在调用adapter.Update(table)
之前,DataTable
中的列在DataRowVersion.Original
和DataRowVersion.Current
我不知道从哪里开始寻找这个,所以我希望有人能把我推向正确的方向
我在所有窗体上使用绑定,每个控件都绑定到一个绑定到DataTable
的BindingSource
以下是我的ApplyUpdate
命令的完整代码:
public int ApplyUpdates(DataTable Table, string SelectTextForUpdate, string IdentityFieldName = "") Command, string SelectTextForUpdate = "")
{
int Result = -1;
if (_ConnectionString != null && _ConnectionString != "")
{
using (SqlConnection connection = new SqlConnection(_ConnectionString))
{
connection.Open();
SqlTransaction trans = connection.BeginTransaction();
using (SqlDataAdapter adapter = new SqlDataAdapter())
{
using (SqlCommand command = new SqlCommand())
{
using (SqlCommandBuilder builder = new SqlCommandBuilder())
{
adapter.SelectCommand = command;
adapter.SelectCommand.Connection = connection;
builder.DataAdapter = adapter;
// Make only fields with changed values appear in the update command
// !!!!!!!!!!!!!!!!!!!!!!!!! Somewhere in 2016 this stopped working and it always updates all values whatever the value of this property !!!!!!!!!!!!!!!!!!!!!!!!!
builder.SetAllValues = false;
// Make the where clause of the update statement to have only the primary field in it.
builder.ConflictOption = ConflictOption.OverwriteChanges;
adapter.SelectCommand.CommandText = SelectTextForUpdate;
adapter.SelectCommand.Transaction = trans;
adapter.UpdateCommand = builder.GetUpdateCommand(true).Clone();
adapter.DeleteCommand = builder.GetDeleteCommand(true).Clone();
// create new insertcommand with extra parameter for getting the stupid identity field
SqlCommand inserter = new SqlCommand();
inserter = builder.GetInsertCommand(true).Clone();
if (IdentityFieldName != "")
{
inserter.CommandText += " SET @ID = SCOPE_IDENTITY()";
SqlParameter param = new SqlParameter();
param.Direction = ParameterDirection.Output;
param.Size = 4;
param.DbType = DbType.Int32;
param.ParameterName = "@ID";
inserter.Parameters.Add(param);
}
//put custom insertcommand into our adapter
adapter.InsertCommand = inserter;
// now dispose the original CommandBuilder. The original builder is bound to our adapter and will overwrite the insertcommand
// just before the adapter.update command, and thus dump your new parameter...
// The only way to break this evil spell is to dispose the bad sorcerer
builder.Dispose();
// now create a temperary RowUpdated event, in this we can update the identity field of the table
adapter.RowUpdated += adapter_RowUpdated;
_Table = Table;
_IdentityFieldName = IdentityFieldName;
try
{
try
{
if (adapter.InsertCommand != null)
adapter.InsertCommand.Transaction = trans;
if (adapter.UpdateCommand != null)
adapter.UpdateCommand.Transaction = trans;
if (adapter.DeleteCommand != null)
adapter.DeleteCommand.Transaction = trans;
Result = adapter.Update(Table);
trans.Commit();
}
catch (Exception ex)
{
trans.Rollback();
throw new Exception(ex.Message);
}
}
finally
{
// get rid of the temperay RowUpdated event
adapter.RowUpdated -= adapter_RowUpdated;
_Table = null;
_IdentityFieldName = "";
inserter.Dispose();
}
Table.AcceptChanges();
}
}
}
}
}