• <fieldset id="8imwq"><menu id="8imwq"></menu></fieldset>
  • <bdo id="8imwq"><input id="8imwq"></input></bdo>
    最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
    問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
    當前位置: 首頁 - 科技 - 知識百科 - 正文

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

    來源:懂視網 責編:小采 時間:2020-11-27 22:40:42
    文檔

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

    DataAdapter執行批量更新的實例代碼:在以前版本的 ADO.NET 中,使用 DataSet 中的更改來更新數據庫時,DataAdapter 的 Update 方法每次更新數據庫的一行。因為該方法循環訪問指定 DataTable 中的行,所以,會檢查每個 DataRow,確定是否已修改。如果該行已修改,將根據該行的 RowState
    推薦度:
    導讀DataAdapter執行批量更新的實例代碼:在以前版本的 ADO.NET 中,使用 DataSet 中的更改來更新數據庫時,DataAdapter 的 Update 方法每次更新數據庫的一行。因為該方法循環訪問指定 DataTable 中的行,所以,會檢查每個 DataRow,確定是否已修改。如果該行已修改,將根據該行的 RowState

    在以前版本的 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>";
        }

    聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

    文檔

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

    DataAdapter執行批量更新的實例代碼:在以前版本的 ADO.NET 中,使用 DataSet 中的更改來更新數據庫時,DataAdapter 的 Update 方法每次更新數據庫的一行。因為該方法循環訪問指定 DataTable 中的行,所以,會檢查每個 DataRow,確定是否已修改。如果該行已修改,將根據該行的 RowState
    推薦度:
    • 熱門焦點

    最新推薦

    猜你喜歡

    熱門推薦

    專題
    Top
    主站蜘蛛池模板: 久久无码精品一区二区三区| 亚洲伊人久久精品影院| 亚洲日韩国产AV无码无码精品| 97久久精品午夜一区二区| 欧美精品亚洲精品日韩专区| 香蕉国产精品频视| 2021最新国产精品网站| 亚洲av永久无码精品漫画| 精品一区二区三区免费视频| 久久国产亚洲精品麻豆| 国产精品一区二区av| 无码人妻精品一区二区三区99仓本| 久久se精品一区二区影院 | 久久成人国产精品| 午夜国产精品无套| 久久99精品国产99久久6| 国产成人精品a视频一区| 久久免费精品一区二区| 国产精品亚洲欧美一区麻豆| 国产精品jizz视频| 精品国际久久久久999波多野| 无码精品国产VA在线观看DVD| 亚洲国产精品国产自在在线| 日本精品少妇一区二区三区| 精品久久久久久国产牛牛app| 国产精品乱伦| 国产高清在线精品一区小说| 99免费精品国产| 国产精品成人久久久久久久| 国产精品99久久久久久猫咪| 国产成人精品曰本亚洲79ren| 92精品国产自产在线观看| 亚洲国语精品自产拍在线观看| 久久国产精品久久| 久久久精品免费国产四虎| 色播精品免费小视频| 91精品视频在线| 国产精品视频九九九| 精品无码久久久久久久久久| 欧美亚洲日本久久精品| 亚洲韩精品欧美一区二区三区|