国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - ASP.NET教程 - DataAdapter執行批量更新的實例代碼

DataAdapter執行批量更新的實例代碼

2019-11-14 14:25ASP.NET教程網 ASP.NET教程

這篇文章介紹了DataAdapter執行批量更新的實例代碼,有需要的朋友可以參考一下,希望對你有所幫助

在以前版本的 ADO.NET 中,使用 DataSet 中的更改來更新數據庫時,DataAdapter 的 Update 方法每次更新數據庫的一行。因為該方法循環訪問指定 DataTable 中的行,所以,會檢查每個 DataRow,確定是否已修改。如果該行已修改,將根據該行的 RowState 屬性值調用相應的 UpdateCommand、InsertCommand 或 DeleteCommand。每一次行更新都涉及網絡與數據庫之間的雙向數據傳輸。
    在 ADO.NET 2.0 中,DataAdapter 公開了 UpdateBatchSize 屬性。將 UpdateBatchSize 設置為正整數值將使對數據庫的更新以指定大小的批次進行發送。例如,如果將 UpdateBatchSize 設置為 10,會將 10 個獨立的語句組合在一起并作為一批提交。將 UpdateBatchSize 設置為 0 將導致 DataAdapter 使用服務器可以處理的最大批次的大小。如果將其設置為 1,則禁用批量更新,因為此時每次發送一行。
    執行非常大的批次可能會降低性能。因此,在實現應用程序之前,應測試最佳的批次大小設置。
    使用 UpdateBatchSize 屬性
    啟用了批量更新后,DataAdapter 的 UpdateCommand、InsertCommand 和 DeleteCommand 的 UpdatedRowSource 屬性值應設置為 None 或 OutputParameters。在執行批量更新時,命令的 FirstReturnedRecord 或 Both 的 UpdatedRowSource 屬性值無效。
    下面的過程演示如何使用 UpdateBatchSize 屬性。該過程采用兩個參數,一個 DataSet 對象,其中包含代表 PRoduction.ProductCategory 表中的 ProductCategoryID 和 Name 字段的列,一個代表批次大小的整數(批次中的行數)。該代碼創建一個新的 SqlDataAdapter 對象,設置其 UpdateCommand、InsertCommand 和 DeleteCommand 屬性。該代碼假定 DataSet 對象已修改了行。它設置 UpdateBatchSize 屬性并執行更新。

復制代碼代碼如下:


    protected void btnUpdateAddress_Click(object sender, EventArgs e)
    {
    SqlDataAdapter EmpAdapter = new SqlDataAdapter();
    DataTable EmpDT = new DataTable();
    SqlConnection DBConSelect = new SqlConnection();
    SqlConnection DBConUpdate = new SqlConnection();
    SqlCommand SelectCommand = new SqlCommand();
    SqlCommand UpdateCommand = new SqlCommand();
    // Using different connection objects for select and updates from the
    // Northwind database.
    DBConSelect.ConnectionString =
    ConfigurationManager.ConnectionStrings["DSN_NorthWind"].ConnectionString;
    DBConUpdate.ConnectionString =
    ConfigurationManager.ConnectionStrings["DSN_NorthWind"].ConnectionString;
    // Reading all records from the Employees table
    SelectCommand.CommandText = "SELECT top 500 * FROM EMPLOYEES";
    SelectCommand.CommandType = CommandType.Text;
    SelectCommand.Connection = DBConSelect;

 

 UpdateCommand.CommandText = " UPDATE EMPLOYEES SET Address=@Address, " +
    "City=@City, Region=@Region, Country=@Country";
    UpdateCommand.CommandType = CommandType.Text;
    UpdateCommand.Connection = DBConUpdate;
    SqlParameter AddressParam;
    AddressParam = new SqlParameter("@Address",
    SqlDbType.VarChar, 15, "Address");
    SqlParameter CityParam;
    CityParam = new SqlParameter("@City", SqlDbType.VarChar, 15, "City");
    SqlParameter RegionParam;
    RegionParam = new SqlParameter("@Region", SqlDbType.VarChar, 15, "Region");
    SqlParameter CountryParam;
    CountryParam = new SqlParameter("@Country",
    SqlDbType.VarChar, 15, "Country");
    UpdateCommand.Parameters.Add(AddressParam);
    UpdateCommand.Parameters.Add(CityParam);
    UpdateCommand.Parameters.Add(RegionParam);
    UpdateCommand.Parameters.Add(CountryParam);
    // Setting up Data Adapter with the Select and Update Commands
    // The Select command will be used to retrieve all employee
    // information from the Northwind database and the Update command
    // will be used to save changes back to the database
    EmpAdapter.SelectCommand = SelectCommand;
    EmpAdapter.UpdateCommand = UpdateCommand;
    EmpAdapter.Fill(EmpDT);
    DBConSelect.Close();
    // Looping through all employee records and assigning them the new
    // address
    foreach (DataRow DR in EmpDT.Rows)
    {
    DR["Address"] = "4445 W 77th Street, Suite 140";
    DR["City"] = "Edina";
    DR["Region"] = "Minnesota";
    DR["Country"] = "USA";
    }
    // Adding an event handler to listen to the RowUpdated event.
    // This event will will fire after each batch is executed
    EmpAdapter.RowUpdated +=  new SqlRowUpdatedEventHandler(OnRowUpdated);
    lblCounter.Text = "";
    EmpAdapter.UpdateBatchSize = 100;
    // It is important to set this property for batch processing of
    // updated records since batch updates are incapable of
    // updating the source with changes from the database
    UpdateCommand.UpdatedRowSource = UpdateRowSource.None;
    try
    {
    DBConUpdate.Open();
    EmpAdapter.Update(EmpDT);
    }
    catch (Exception ex)
    {
    lblCounter.Text += ex.Message + "<Br>";
    }
    finally
    {
    if (DBConUpdate.State == ConnectionState.Open)
    {
    DBConUpdate.Close();
    }
    }
    }
    private void OnRowUpdated(object sender, SqlRowUpdatedEventArgs args)
    {
    lblCounter.Text += "Batch is processed till row number = " +
    args.RowCount.ToString() + "<br>";
    }

 

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚洲精品久久久久久一区二区 | 性欧美成人播放77777 | 99久久99久久久精品色圆 | 欧美日韩中文字幕在线 | 午夜在线电影 | 亚洲欧美激情在线 | 免费看黄的视频网站 | 久精品视频 | 成人乱人乱一区二区三区 | 欧美日韩久| 成人性做爰av片免费看 | 国产精品久久久久久亚洲调教 | 久久av综合 | 日韩av一区在线 | 午夜视频在线播放 | www.爱色av.com | 亚洲精品国产第一综合99久久 | 成人小视频在线看 | 国产精品一区二区三区免费 | 一级黄色免费毛片 | 91传媒在线播放 | 99精品国产高清一区二区麻豆 | 精品在线一区二区三区 | 日韩成人一区 | 成人精品一区二区三区中文字幕 | 亚洲精品一区二三区不卡 | 久久99精品久久久久久国产越南 | 日本久久综合 | 欧美日韩激情 | 91在线公开视频 | 国产亚洲成av人片在线观看桃 | 中文字幕在线免费 | 久草中文在线 | 日韩欧美一级片 | 国产精品久久国产精品 | 高清中文字幕 | 中日韩一线二线三线视频 | 久久国内 | 久久久久久久久久久久久av | 日本久久久| 黄色片网站在线免费观看 |