ADO.NET批次更新操作

來源:互聯網
上載者:User

ADO.NET批次更新操作

   批次更新操作

  .在上一個版本的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");

  }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.