国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

使用 DataAdapter 執(zhí)行批量更新

2019-11-18 17:15:46
字體:
供稿:網(wǎng)友

   摘自MSDN:
在以前版本的 ADO.NET 中,使用 DataSet 中的更改來更新數(shù)據(jù)庫時(shí),DataAdapter 的 Update 方法每次更新數(shù)據(jù)庫的一行。因?yàn)樵摲椒ㄑh(huán)訪問指定 DataTable 中的行,所以,會(huì)檢查每個(gè) DataRow,確定是否已修改。如果該行已修改,將根據(jù)該行的 RowState 屬性值調(diào)用相應(yīng)的 UpdateCommand、InsertCommand 或 DeleteCommand。每一次行更新都涉及網(wǎng)絡(luò)與數(shù)據(jù)庫之間的雙向數(shù)據(jù)傳輸。

在 ADO.NET 2.0 中,DataAdapter 公開了 UpdateBatchSize 屬性。將 UpdateBatchSize 設(shè)置為正整數(shù)值將使對數(shù)據(jù)庫的更新以指定大小的批次進(jìn)行發(fā)送。例如,如果將 UpdateBatchSize 設(shè)置為 10,會(huì)將 10 個(gè)獨(dú)立的語句組合在一起并作為一批提交。將 UpdateBatchSize 設(shè)置為 0 將導(dǎo)致 DataAdapter 使用服務(wù)器可以處理的最大批次的大小。如果將其設(shè)置為 1,則禁用批量更新,因?yàn)榇藭r(shí)每次發(fā)送一行。

執(zhí)行非常大的批次可能會(huì)降低性能。因此,在實(shí)現(xiàn)應(yīng)用程序之前,應(yīng)測試最佳的批次大小設(shè)置。

使用 UpdateBatchSize 屬性
啟用了批量更新后,DataAdapter 的 UpdateCommand、InsertCommand 和 DeleteCommand 的 UpdatedRowSource 屬性值應(yīng)設(shè)置為 None 或 OutputParameters。在執(zhí)行批量更新時(shí),命令的 FirstReturnedRecord 或 Both 的 UpdatedRowSource 屬性值無效。

下面的過程演示如何使用 UpdateBatchSize 屬性。該過程采用兩個(gè)參數(shù),一個(gè) DataSet 對象,其中包含代表 PRoduction.ProductCategory 表中的 ProductCategoryID 和 Name 字段的列,一個(gè)代表批次大小的整數(shù)(批次中的行數(shù))。該代碼創(chuàng)建一個(gè)新的 SqlDataAdapter 對象,設(shè)置其 UpdateCommand、InsertCommand 和 DeleteCommand 屬性。該代碼假定 DataSet 對象已修改了行。它設(shè)置 UpdateBatchSize 屬性并執(zhí)行更新。

 


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


發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 偏关县| 庆云县| 皋兰县| 社旗县| 尉犁县| 毕节市| 云林县| 汝阳县| 沛县| 监利县| 庄浪县| 武安市| 天长市| 鹤山市| 洪雅县| 武功县| 荥经县| 常宁市| 太仆寺旗| 湄潭县| 山东省| 瑞金市| 镇雄县| 元谋县| 逊克县| 丰原市| 浦县| 抚顺市| 木里| 江源县| 崇仁县| 大连市| 木里| 泽州县| 泗洪县| 柯坪县| 吉隆县| 华容县| 巴林右旗| 南和县| 武强县|