首 页 网络编程
网页制作 图形图象 操作系统 冲浪宝典
软件教学 认证考试

网络安全 网络办公 行业资讯 评测对比
您当前位置:站长天空 -> 软件教学-> 图文处理
使用 dataadapter 执行批量更新_ado.net应用
作者:网友供稿 点击:0
推荐
西部数码-全国虚拟主机10强!20余项虚拟主机管理功能,全国领先!第6代双线路虚拟主机,南北访问畅通无阻!可在线rar解压,自动数据恢复设置虚拟目录等.免费赠送访问统计,企业邮局.Cn域名注册10元/年,自助建站480元起,免费试用7天,满意再付款!P4主机租用799元/月.月付免压金
站内搜索
文章页数:[1] 

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


文章整理:站长天空 网址:http://www.z6688.com/
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

文章页数:[1] 


放大字体显示 缩小字体显示 打印文章 推荐给朋友
热门文章
·Java Reflection (JAVA反射)-JSP教程,Java技巧及代码
·WebServices入门-.NET教程,Web Service开发
·如何在c#中使用mapx5.0(二)-.NET教程,C#语言
·用java启动记事本程序,并输入内容-JSP教程,Java技巧及代码
·正则表达式-ASP教程,正则表达式
·sql server中的外键约束-数据库专栏,SQL Server
·java中的基于套结字(socket)的通信 一. 一个双人单方向通信例子-JSP教程,Java技巧及代码
·图解利用Eclipse3+Lomboz3+Tomcat开发JSP --1.环境-JSP教程,Jsp/Servlet
·EditPlus+NAnt构建轻巧的.NET开发环境-.NET教程,Asp.Net开发
·ASP.NET2.0下利用javascript实现TreeView中的checkbox全选-.NET教程,Asp.Net开发
最新文章
·photoshop简单为美女照片瘦身及调色_photoshop教程
·fireworks巧绘精美可爱水杯图标_fireworks教程
·如何修复windows vista系统用户损坏的配置文件_windows vista
·本地门户营运的3个重点注意事项_站长心得
·必知:在线推广网站的十四种方法_站长心得
·google analytics统计分析新版本说明_google推广
·google adsense 违规行为集_google推广
·googleadsense的无效点击_google推广
·关于网络原创作品的版权问题_网络编辑
·符合xhtml标准div+css布局的网站对网站seo的影响_seo网站优化
相关主题
  • 使用 DataAdapter 和 DataSet 更新数据库-.NET教程,数据库应用
  • 使用 DataAdapter 和 DataSet 更新数据库 [C#]-.NET教程,数据库应用
  • 西部数码虚拟主机

    友情链接
    CNNIC 西部数码
    万网 自助建站
    虚拟主机 asp空间
    域名注册 域名
    域名申请 主页空间
    论坛空间 网站空间
    国际域名 虚拟空间
    空间租用 DDOS防火墙
    成都主机托管 四川主机托管
    主机租用 服务器租用
    网站目录 自助建站
    虚拟主机 网址大全
    软件下载
    自助链接
    虚拟主机资讯 特价虚拟主机
    版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!
    关于我们:站长天空:专业提供最新的站长资讯、在线教程、虚拟主机权威评测、虚拟主机性能对比、网站制作教程,开发教程,站长工具。包括网页制作教程、冲浪宝典、编程参考、操作系统、软件教学、行业动态等。
    特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。
    发表评论 打印  刷新     关闭