batch update For Sql

本文介绍了如何在ADO.NET 2.0中使用DataAdapter对象进行批量插入操作,适用于.NET 2.0环境和SQL Server 2000及以上版本。通过设置InsertCommand为存储过程,并指定UpdateBatchSize,可以在一次网络往返中处理多个插入操作。示例代码展示了创建存储过程、设置参数和执行批量插入的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Use Batch Update of ADO.NET 2.0 DataAdapter Object

Valid for Environment: .NET 2.0 (ADO.NET 2.0), SQL Server 2000 or above

It was quite a cumbersome job until ADO.NET 2.0. But with ADO.NET 2.0 things got very simple as Adapter now supports multiple Insert/Update with the user defined batch size. We are going to limit our discussion to Insert functionality. For Insert, create the Command object with usual simple stored proc for Insert and specify that as the InsertCommand to the DataAdapter object. Along with this we need to specify the UpdateBatchSize which determines the number of Inserts to be processed in one network round trip. Follow the code below to have complete understanding.

First of all create the stored proc in your SQL Server Instance:

CREATE PROCEDURE sp_BatchInsert ( @PersonId INT, @PersonName VARCHAR(100) )
AS
BEGIN
INSERT INTO Person VALUES ( @PersonId, @PersonName);
END

Now refer to the C# code below:

private void btnBatchInsert_Click(object sender, EventArgs e)

{

// Get the DataTable with Rows State as RowState.Added

DataTable dtInsertRows = GetDataTable();

 

SqlConnection connection = new SqlConnection(connectionString);

SqlCommand command = new SqlCommand("sp_BatchInsert", connection);

command.CommandType = CommandType.StoredProcedure;

command.UpdatedRowSource = UpdateRowSource.None;

 

// Set the Parameter with appropriate Source Column Name

command.Parameters.Add("@PersonId", SqlDbType.Int, 4, dtInsertRows.Columns[0].ColumnName);

command.Parameters.Add("@PersonName", SqlDbType.VarChar, 100, dtInsertRows.Columns[1].ColumnName);

 

SqlDataAdapter adpt = new SqlDataAdapter();

adpt.InsertCommand = command;

// Specify the number of records to be Inserted/Updated in one go. Default is 1.

adpt.UpdateBatchSize = 2;

 

connection.Open();

int recordsInserted = adpt.Update(dtInsertRows);

connection.Close();

 

MessageBox.Show("Number of records affected : " + recordsInserted.ToString());

 

}

Well, first thing we all developers do is check using SQL Profiler. And to our surprise it shows 4 different RPC requests sent to SQL Server. This is how it looks there :

Do not Panic. The difference in not in the way your statements are executed at the Server, the difference is in terms of how your request(s) are sent to the server. In simple words, with UpdateBatchSize as 2 ( in this case ), it just means that request for insertion of 2 rows will be grouped together and sent to the database server in single network round trip. You can probably use some other tools like “Netmon” etc to have a closer look. So use the UpdateBatchSize appropriately

The DataAdapter has two update-related events: RowUpdating and RowUpdated. Only one RowUpdated event occurs for each batch, whereas the RowUpdating event occurs for each row processed. You may also like to look at the Exceptional handling part of it. Explore them.

 

-----reference the Neeraj Saluja

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值