Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
SqlCommandBuilder class
Message
From
14/04/2014 13:57:47
 
General information
Forum:
ASP.NET
Category:
ADO.NET
Environment versions
Environment:
C# 4.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Application:
Web
Miscellaneous
Thread ID:
01598513
Message ID:
01598616
Views:
26
>>>Although it did shorten the WHERE clause as I see. But it included all columns in the update, not only the changed values.
>>
>>Add an event handler for the data adapter's OnRowUpdating event (http://msdn.microsoft.com/en-us/library/system.data.common.dbdataadapter.onrowupdating%28v=vs.110%29.aspx). Check the command used there and verify whether or not the values in the columns changed. Also make sure the data adapter's UpdateBatchSize is set to 1.
>
>No change :( I added OnRowUpdating method and I set the batch size. I still see the exact same command in the OnRowUpdating method.
>
>Here is my current code:
>
>
>  protected Boolean ProcessDataTable(SqlCommand toSqlCommand)
>        {
>            Boolean results = false;
>
>            try
>            {
>                this.table = new DataTable();
>                this.adapter = new SqlDataAdapter(toSqlCommand);
>                this.adapter.SelectCommand = toSqlCommand;
>                this.adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey; // Generate PK information
>                this.adapter.Fill(this.table);
>                this.adapter.RowUpdating += new SqlRowUpdatingEventHandler(OnRowUpdating);
>                
>                results = true;
>            }
>            catch (Exception ex)
>            {
>                results = false;
>                Logging.LogFormat(2, ex.Message);  // at least log the problem
>            }
>
>            return results;
>        }
>        // handler for RowUpdating event 
>        private static void OnRowUpdating(object sender, SqlRowUpdatingEventArgs e)
>        {
>            PrintEventArgs(e);
>        }
>
>        private static void PrintEventArgs(SqlRowUpdatingEventArgs args)
>        {
>            Console.WriteLine("OnRowUpdating");
>            Console.WriteLine("  event args: (" +
>                " command=" + args.Command +
>                " commandType=" + args.StatementType +
>                " status=" + args.Status + ")");
>        }
>        /// <summary>
>        /// Update information
>        /// </summary>
>        /// <returns></returns>
>        public Boolean UpdateRows(Boolean generateCommands = false)
>        {
>            Boolean results = false;
>            try
>            {
>                if (generateCommands)
>                {
>                   
>                    this.adapter.UpdateBatchSize = 1; //Disable batch processing
>                    SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
>                    builder.ConflictOption = ConflictOption.OverwriteChanges;
>                    builder.SetAllValues = false; // Set only changed values
>                    adapter.UpdateCommand = builder.GetUpdateCommand(true);
>                }
>                adapter.Update(this.table);
>                results = true;
>            }
>
>            catch (Exception ex)
>            {
>                results = false;
>                Logging.LogFormat(2, ex.Message);  // at least log the problem
>            }
>
>            return results;
>        }
>
>Anything else I am forgetting?
>
>Thanks again.

Try not setting the adapter.UpdateCommand. See remarks section under http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldataadapter.updatecommand%28v=vs.110%29.aspx and the fifth paragraph at http://msdn.microsoft.com/en-us/library/tf579hcz%28v=vs.110%29.aspx for reasons why it shouldn't be set.
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform