ADO.NET批量更新操作

来源:网络时间:2011-06-23 14:02:22

  批量更新操作

  .在上一个版本的ADO.NET当中,SqlDataAdapterde的Update方法将会为 DataSet当中的每一行调用一次更新操作

  .在ADO.NET2.0中,您可以设置UpdateBatchSize属性,在单步中执行多个更新

  .这样,可以提高数据更新的效率

  .UpdataBatchSize的默认值为1,使得默认的更新行为与以前版本的ADO.NET一致。

  代码经验

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

  }

文章内容来源于网络,不代表本站立场,若侵犯到您的权益,可联系我们删除。(本站为非盈利性质网站) 联系邮箱:9145908@qq.com