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

首頁 > 開發 > 綜合 > 正文

使用 DataAdapter 執行批量更新

2024-07-21 02:29:09
字體:
來源:轉載
供稿:網友

   摘自msdn:

  在以前版本的 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 [email protected], " +
                                    "[email protected], [email protected], [email protected]";

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 防城港市| 平果县| 台安县| 嫩江县| 宣化县| 七台河市| 竹溪县| 阳城县| 华安县| 白河县| 昭觉县| 启东市| 京山县| 广丰县| 都昌县| 吉林省| 祁连县| 略阳县| 泾源县| 佛冈县| 阿拉善左旗| 荃湾区| 弋阳县| 赤壁市| 阳曲县| 阜宁县| 汉川市| 稻城县| 乐亭县| 灵璧县| 高安市| 宁化县| 普兰店市| 南宫市| 苍溪县| 进贤县| 银川市| 徐水县| 建平县| 东宁县| 昔阳县|