DataAdapter performs batch updates of the instance code

  • 2020-06-23 00:08:25
  • OfStack

In previous versions of ES1en.NET, the Update method of DataAdapter updated the database 1 row at a time when changes in DataSet were used to update the database. Because this method iterates through the rows in the specified DataTable, each DataRow is checked for changes. If the row is modified, the corresponding UpdateCommand, InsertCommand, or DeleteCommand are called based on the RowState attribute value of the row. Each row update involves a two-way data transfer between the network and the database.
In ES13en.NET 2.0, DataAdapter exposes the UpdateBatchSize attribute. Setting UpdateBatchSize to a positive integer value causes updates to the database to be sent in batches of the specified size. For example, if UpdateBatchSize is set to 10, 10 separate statements are combined at 1 and submitted as a batch. Setting UpdateBatchSize to 0 causes DataAdapter to use the maximum batch size that the server can handle. If it is set to 1, batch updates are disabled, because one row is sent at a time.
Performing very large batches can degrade performance. Therefore, the optimal batch size setting should be tested before implementing the application.
Use the UpdateBatchSize attribute
With batch updates enabled, the UpdatedRowSource attribute values for DataAdapter's UpdateCommand, InsertCommand, and DeleteCommand should be set to None or OutputParameters. The FirstReturnedRecord or UpdatedRowSource attribute value of the command is invalid when performing bulk updates.
The following procedure demonstrates how to use the UpdateBatchSize attribute. The process takes two parameters, an DataSet object containing columns representing the ProductCategoryID and Name fields in the ES38en.ProductCategory table, and an integer representing the batch size (number of rows in a batch). This code creates a new SqlDataAdapter object and sets its UpdateCommand, InsertCommand, and DeleteCommand properties. This code assumes that the DataSet object has modified the line. It sets the UpdateBatchSize property and performs the update.


    protected void btnUpdateAddress_Click ( object sender, EventArgs e ) 
    {
    SqlDataAdapter EmpAdapter = new SqlDataAdapter (a); 
    DataTable EmpDT = new DataTable (a); 
    SqlConnection DBConSelect = new SqlConnection (a); 
    SqlConnection DBConUpdate = new SqlConnection (a); 
    SqlCommand SelectCommand = new SqlCommand (a); 
    SqlCommand UpdateCommand = new SqlCommand (a); 
    // Using different connection objects for select and updates from the
    // Northwind database.
    DBConSelect.ConnectionString =
    ConfigurationManager.ConnectionStrings["DSN_NorthWind"].ConnectionString;
    DBConUpdate.ConnectionString =
    ConfigurationManager.ConnectionStrings["DSN_NorthWind"].ConnectionString;
    // Reading all records from the Employees table
    SelectCommand.CommandText = "SELECT top 500 * FROM EMPLOYEES";
    SelectCommand.CommandType = CommandType.Text;
    SelectCommand.Connection = DBConSelect;
 UpdateCommand.CommandText = " UPDATE EMPLOYEES SET Address=@Address, " +
    "City=@City, Region=@Region, Country=@Country";
    UpdateCommand.CommandType = CommandType.Text;
    UpdateCommand.Connection = DBConUpdate;
    SqlParameter AddressParam;
    AddressParam = new SqlParameter ( "@Address",
    SqlDbType.VarChar, 15, "Address" ); 
    SqlParameter CityParam;
    CityParam = new SqlParameter ( "@City", SqlDbType.VarChar, 15, "City" ); 
    SqlParameter RegionParam;
    RegionParam = new SqlParameter ( "@Region", SqlDbType.VarChar, 15, "Region" ); 
    SqlParameter CountryParam;
    CountryParam = new SqlParameter ( "@Country",
    SqlDbType.VarChar, 15, "Country" ); 
    UpdateCommand.Parameters.Add ( AddressParam ); 
    UpdateCommand.Parameters.Add ( CityParam ); 
    UpdateCommand.Parameters.Add ( RegionParam ); 
    UpdateCommand.Parameters.Add ( CountryParam ); 
    // Setting up Data Adapter with the Select and Update Commands
    // The Select command will be used to retrieve all employee
    // information from the Northwind database and the Update command
    // will be used to save changes back to the database
    EmpAdapter.SelectCommand = SelectCommand;
    EmpAdapter.UpdateCommand = UpdateCommand;
    EmpAdapter.Fill ( EmpDT ); 
    DBConSelect.Close (a); 
    // Looping through all employee records and assigning them the new
    // address
    foreach  ( DataRow DR in EmpDT.Rows ) 
    {
    DR["Address"] = "4445 W 77th Street, Suite 140";
    DR["City"] = "Edina";
    DR["Region"] = "Minnesota";
    DR["Country"] = "USA";
    }
    // Adding an event handler to listen to the RowUpdated event.
    // This event will will fire after each batch is executed
    EmpAdapter.RowUpdated +=  new SqlRowUpdatedEventHandler ( OnRowUpdated ); 
    lblCounter.Text = "";
    EmpAdapter.UpdateBatchSize = 100;
    // It is important to set this property for batch processing of
    // updated records since batch updates are incapable of
    // updating the source with changes from the database
    UpdateCommand.UpdatedRowSource = UpdateRowSource.None;
    try
    {
    DBConUpdate.Open (a); 
    EmpAdapter.Update ( EmpDT ); 
    }
    catch  ( Exception ex ) 
    {
    lblCounter.Text += ex.Message + "<Br>";
    }
    finally
    {
    if  ( DBConUpdate.State == ConnectionState.Open ) 
    {
    DBConUpdate.Close (a); 
    }
    }
    }
    private void OnRowUpdated ( object sender, SqlRowUpdatedEventArgs args ) 
    {
    lblCounter.Text += "Batch is processed till row number = " +
    args.RowCount.ToString (a)  + "<br>";
    }


Related articles: