Improve performance using ADO. NET 2.0 batch update feature

Source: Internet
Author: User
Introduction

When you use SqlDataAdapter for processing Ming updates, the SqlDataAdapter propagates the updates one by one. that means if there are 100 rows to be updated the SqlDataAdapter will execute 100 separate operations against the database. as you might have guessed this is not efficient while dealing with large number of rows. fortunately SqlDataAdapter allows you to execute updates in batches. you can specify the batch size I. e. number of rows to be treated as a single batch via UpdateBatchSize property.

There are also two events associated with this that you can use RowUpdating and RowUpdated. in normal situations I. e. in the absence of batch update, both of these events are raised for each and every row being updated. for example, if we are updating 100 rows, both of these rows are raised for 100 times. however, when you turn on the batch update behavior, the RowUpdating event is raised as in previous case but RowUpdated event is raised after the entire batch is updated. if we set the batch size to 10 then RowUpdating will be raised for 100 times where as RowUpdated will be raised only for 10 times.

Following code using strates use of this feature:

Class Program
{
Private static int updating = 0;
Private static int updated = 0;
Static void Main (string [] args)
{
SqlConnection cnn = new SqlConnection (
"Data source =. \ sqlexpress;
Initial catalog = northwind; integrated security = true ");
SqlDataAdapter da = new
SqlDataAdapter ("select * from MERs", cnn );
Da. RowUpdating + = new
SqlRowUpdatingEventHandler (da_RowUpdating );
Da. RowUpdated + = new
SqlRowUpdatedEventHandler (da_RowUpdated );
DataSet ds = new DataSet ();
Da. Fill (ds, "mycustomers ");
Foreach (DataRow row in ds. Tables [0]. Rows)
{
String country = row ["country"]. ToString ();
// Simulate row change
Row ["country"] = country;
}
SqlCommand cmd = new SqlCommand ();
Cmd. Connection = cnn;
Cmd. CommandType = CommandType. Text;
Cmd. CommandText = "update MERs set
Country = @ country where customerid = @ custid ";
SqlParameter p1 = new
SqlParameter ("@ country", SqlDbType. VarChar );
P1.SourceColumn = "country ";
SqlParameter p2 = new
SqlParameter ("@ custid", SqlDbType. VarChar );
P2.SourceColumn = "customerid ";
Cmd. Parameters. Insert (0, p1 );
Cmd. Parameters. Insert (0, p2 );
Cmd. UpdatedRowSource = UpdateRowSource. None;
Da. UpdateCommand = cmd;
Da. UpdateBatchSize = 10;
Da. Update (ds, "mycustomers ");
Console. WriteLine ("Updating:" + updating );
Console. WriteLine ("Updated:" + updated );
Console. ReadLine ();
}
Static void da_RowUpdating
(Object sender, SqlRowUpdatingEventArgs e)
{
Updating ++;
}
Static void da_RowUpdated
(Object sender, SqlRowUpdatedEventArgs e)
{
Updated ++;
}
}

Notice how we have attached event handlers to the RowUpdating and RowUpdated events. these event handlers simply increment two integer variables. then we set the UpdatedDataSource property of the SqlCommand property to enumerated value of UpdateRowSource. none. the UpdatedRowSource property can control how the values returned from the data source are mapped back to the DataSet. setting this property to a value of None is necessary when using batch update feature of DataAdapter. finally, we set the UpdateBatchSize property of the SqlDataAdapter to 10 indicating that we want to update 10 rows as a batch. if you run this application you will notice that the variable updating holds the value equal to the number of rows in the table because this event is raised each time a row is being updated. on the other hand the variable update will hold value equal to (number of rows in the table)/(batch size ).

Summary

In ADO. NET 2.0, the DataAdapter provides a property called UpdateBatchSize which allows you to execute queries in a batch. This significantly CES the database roundtrips and hence is more efficient in terms of performance

Transferred from
Http://www.dotnetbips.com/articles/displayarticle.aspx? Id = 487

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.