1

我为我的SqlCommandbuilder.SetAllValues = false;

几年前,当我实现它时,我使用探查器进行了检查,它工作正常,只有更改的字段在由SqlCommandBuilder.
但现在我发现它不再工作了。
调试时,我注意到在调用之前adapter.Update(table)DataTable列中的值DataRowVersion.OriginalDataRowVersion.Current

我不知道从哪里开始寻找这个,所以我希望有人能把我推向正确的方向。

我在所有表单上都使用绑定,每个控件都绑定到BindingSource绑定到 a 的 a DataTable

这是我的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();
                    }
                }
            }
        }
    }
4

0 回答 0