Ado. NET Bulk Update operation

Source: Internet
Author: User

Bulk Update operations

. In the previous version of Ado.net, the Sqldataadapterde Update method will invoke an update operation for each row in the dataset

. In ado.net2.0, you can set the UpdateBatchSize property to perform multiple updates in one step

. In this way, you can improve the efficiency of data update

. The default value of Updatabatchsize is 1, which makes the default update behavior consistent with previous versions 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.