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");
}