ADO. NET batch update

Source: Internet
Author: User

ADO. NET batch update

Batch update operations

In the previous version of ADO. NET, the Update method of SqlDataAdapterde will call an Update operation for each row in the DataSet.

. In ADO. NET2.0, you can set the UpdateBatchSize attribute and execute multiple updates in a single step.

In this way, the efficiency of data update can be improved.

The default value of. UpdataBatchSize is 1, so that the default update behavior is consistent with that of the previous version of ADO. NET.

Code experience

Public Form1 ()

{

Conn = new SqlConnection (ConfigurationManager. ConnectionStrings ["AWConnectionString"]. ConnectionString );

DAdapt = new SqlDataAdapter ("SELECT ProductID, Name, ListPrice FROM Production. Product", conn );

InitializeComponent ();

}

SqlConnection conn;

SqlDataAdapter dAdapt;

DataSet dSet = new DataSet ();

StringBuilder logString = new StringBuilder ("");

Private void batchUpdateForm_Load (System. Object sender, System. EventArgs e)

{

DAdapt. RowUpdating + = new System. Data. SqlClient. SqlRowUpdatingEventHandler (OnRowUpdating );

DAdapt. RowUpdated + = new System. Data. SqlClient. SqlRowUpdatedEventHandler (OnRowUpdated );

}

Private void getDataButton_Click (System. Object sender, System. EventArgs e)

{

DAdapt. Fill (dSet, "Product ");

ProductGrid. DataSource = dSet. Tables ["Product"];

}

Private void updateDataButton_Click (System. Object sender, System. EventArgs e)

{

SqlCommandBuilder cb = new SqlCommandBuilder (dAdapt );

LogString. Remove (0, logString. Length );

// Enable batching by setting batch size! = 1.

DAdapt. UpdateBatchSize = int. Parse (batchSizeTextBox. Text );

// Execute the update.

DAdapt. Update (dSet. Tables ["Product"]);

MessageBox. Show (logString. ToString ());

}

// Handler for the RowUpdating event

Public void OnRowUpdating (object sender, SqlRowUpdatingEventArgs e)

{

LogString. AppendLine ("Starting row update ");

}

// Handler for RowUpdated event

Public void OnRowUpdated (object sender, SqlRowUpdatedEventArgs e)

{

LogString. AppendLine ("Completed row update ");

}

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.