使用 DataAdapter 执行批量更新 [摘自MSDN]

  摘自MSDN:

在以前版本的 ADO.NET 中,使用 中的更改来更新数据库时,DataAdapter 的 Update 方法每次更新数据库的一行。因为该方法循环访问指定 中的行,所以,会检查每个 ,确定是否已修改。如果该行已修改,将根据该行的 属性值调用相应的 UpdateCommand、InsertCommand 或 DeleteCommand。每一次行更新都涉及网络与数据库之间的双向数据传输。

在 ADO.NET 2.0 中, 公开了 属性。将 UpdateBatchSize 设置为正整数值将使对数据库的更新以指定大小的批次进行发送。例如,如果将 UpdateBatchSize 设置为 10,会将 10 个独立的语句组合在一起并作为一批提交。将 设置为 0 将导致 DataAdapter 使用服务器可以处理的最大批次的大小。如果将其设置为 1,则禁用批量更新,因为此时每次发送一行。

执行非常大的批次可能会降低性能。因此,在实现应用程序之前,应测试最佳的批次大小设置。

使用 UpdateBatchSize 属性

启用了批量更新后,DataAdapter 的 UpdateCommand、InsertCommand 和 DeleteCommand 的 属性值应设置为 或 。在执行批量更新时,命令的 或 的 UpdatedRowSource 属性值无效。

下面的过程演示如何使用 属性。该过程采用两个参数,一个 DataSet 对象,其中包含代表 Production.ProductCategory 表中的 ProductCategoryID 和 Name 字段的列,一个代表批次大小的整数(批次中的行数)。该代码创建一个新的 对象,设置其 、 和 属性。该代码假定 DataSet 对象已修改了行。它设置 UpdateBatchSize 属性并执行更新。


None.gif protected   void  btnUpdateAddress_Click( object  sender, EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        SqlDataAdapter EmpAdapter 
= new SqlDataAdapter();
InBlock.gif        DataTable EmpDT 
= new DataTable();
InBlock.gif        SqlConnection DBConSelect 
= new SqlConnection();
InBlock.gif        SqlConnection DBConUpdate 
= new SqlConnection();
InBlock.gif        SqlCommand SelectCommand 
= new SqlCommand();
InBlock.gif        SqlCommand UpdateCommand 
= new SqlCommand();
InBlock.gif
InBlock.gif        
// Using different connection objects for select and updates from the 
InBlock.gif        
// Northwind database. 
InBlock.gif
        DBConSelect.ConnectionString = 
InBlock.gif          ConfigurationManager.ConnectionStrings[
"DSN_NorthWind"].ConnectionString;
InBlock.gif        DBConUpdate.ConnectionString 
= 
InBlock.gif          ConfigurationManager.ConnectionStrings[
"DSN_NorthWind"].ConnectionString;
InBlock.gif
InBlock.gif        
// Reading all records from the Employees table
InBlock.gif
        SelectCommand.CommandText = "SELECT top 500 * FROM EMPLOYEES";
InBlock.gif        SelectCommand.CommandType 
= CommandType.Text;
InBlock.gif        SelectCommand.Connection 
= DBConSelect;
InBlock.gif
InBlock.gif        UpdateCommand.CommandText 
= " UPDATE EMPLOYEES SET Address=@Address, " + 
InBlock.gif                                    
"City=@City, Region=@Region, Country=@Country";
InBlock.gif
InBlock.gif        UpdateCommand.CommandType 
= CommandType.Text;
InBlock.gif        UpdateCommand.Connection 
= DBConUpdate;
InBlock.gif
InBlock.gif        SqlParameter AddressParam;
InBlock.gif        AddressParam 
= new SqlParameter("@Address"
InBlock.gif           SqlDbType.VarChar, 
15"Address");
InBlock.gif
InBlock.gif        SqlParameter CityParam;
InBlock.gif        CityParam 
= new SqlParameter("@City", SqlDbType.VarChar, 15"City");
InBlock.gif
InBlock.gif        SqlParameter RegionParam;
InBlock.gif        RegionParam 
= new SqlParameter("@Region", SqlDbType.VarChar, 15"Region");
InBlock.gif
InBlock.gif        SqlParameter CountryParam;
InBlock.gif        CountryParam 
= new SqlParameter("@Country"
InBlock.gif           SqlDbType.VarChar, 
15"Country");
InBlock.gif
InBlock.gif        UpdateCommand.Parameters.Add(AddressParam);
InBlock.gif        UpdateCommand.Parameters.Add(CityParam);
InBlock.gif        UpdateCommand.Parameters.Add(RegionParam);
InBlock.gif        UpdateCommand.Parameters.Add(CountryParam);
InBlock.gif
InBlock.gif        
// Setting up Data Adapter with the Select and Update Commands
InBlock.gif        
// The Select command will be used to retrieve all employee
InBlock.gif        
// information from the Northwind database and the Update command
InBlock.gif        
// will be used to save changes back to the database
InBlock.gif
        EmpAdapter.SelectCommand = SelectCommand;
InBlock.gif        EmpAdapter.UpdateCommand 
= UpdateCommand;
InBlock.gif
InBlock.gif        EmpAdapter.Fill(EmpDT);
InBlock.gif
InBlock.gif        DBConSelect.Close();
InBlock.gif
InBlock.gif        
// Looping through all employee records and assigning them the new 
InBlock.gif        
// address
InBlock.gif
        foreach (DataRow DR in EmpDT.Rows)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            DR[
"Address"= "4445 W 77th Street, Suite 140";
InBlock.gif            DR[
"City"= "Edina";
InBlock.gif            DR[
"Region"= "Minnesota";
InBlock.gif            DR[
"Country"= "USA";
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
// Adding an event handler to listen to the RowUpdated event.
InBlock.gif        
// This event will will fire after each batch is executed
InBlock.gif
        EmpAdapter.RowUpdated +=  new SqlRowUpdatedEventHandler(OnRowUpdated);
InBlock.gif
InBlock.gif        lblCounter.Text 
= "";
InBlock.gif
InBlock.gif        EmpAdapter.UpdateBatchSize 
= 100;
InBlock.gif
InBlock.gif        
// It is important to set this property for batch processing of 
InBlock.gif        
// updated records since batch updates are incapable of 
InBlock.gif        
// updating the source with changes from the database
InBlock.gif
        UpdateCommand.UpdatedRowSource = UpdateRowSource.None;
InBlock.gif
InBlock.gif        
try
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            DBConUpdate.Open();
InBlock.gif            EmpAdapter.Update(EmpDT);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
catch (Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            lblCounter.Text 
+= ex.Message + "<Br>";
ExpandedSubBlockEnd.gif        }

InBlock.gif        
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (DBConUpdate.State == ConnectionState.Open)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                DBConUpdate.Close();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

None.gif
None.gif    
private   void  OnRowUpdated( object  sender, SqlRowUpdatedEventArgs args)
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        lblCounter.Text 
+= "Batch is processed till row number = " + 
InBlock.gif           args.RowCount.ToString() 
+ "<br>";
ExpandedBlockEnd.gif    }

转载于:https://www.cnblogs.com/SoulStore/archive/2007/04/11/708409.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值