Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
SqlCommandBuilder class
Message
 
 
À
14/04/2014 13:07:06
Information générale
Forum:
ASP.NET
Catégorie:
ADO.NET
Versions des environnements
Environment:
C# 4.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Application:
Web
Divers
Thread ID:
01598513
Message ID:
01598614
Vues:
23
>>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.
If it's not broken, fix it until it is.


My Blog
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform