SqlBulkCopy.BatchSize Property


Number of rows in each batch. At the end of each batch, the rows in the batch are sent to the server.

Namespace:   System.Data.SqlClient
Assembly:  System.Data (in System.Data.dll)

public int BatchSize { get; set; }

Property Value

Type: System.Int32

The integer value of the BatchSize property, or zero if no value has been set.

A batch is complete when BatchSize rows have been processed or there are no more rows to send to the destination data source.

Zero (the default) indicates that each WriteToServer operation is a single batch.

If the SqlBulkCopy instance has been declared without the UseInternalTransaction option in effect, rows are sent to the server BatchSize rows at a time, but no transaction-related action is taken. If UseInternalTransaction is in effect, each batch of rows is inserted as a separate transaction.

The BatchSize property can be set at any time. If a bulk copy is already in progress, the current batch is sized according to the previous batch size. Subsequent batches use the new size. If the BatchSize is initially zero and changed while a WriteToServer operation is already in progress, that operation loads the data as a single batch. Any subsequent WriteToServer operations on the same SqlBulkCopy instance use the new BatchSize.

The following console application demonstrates how to bulk load data in batches of 50 rows. For an example illustrating how BatchSize works with a transaction, see Transaction and Bulk Copy Operations.

In this example, the source data is first read from a SQL Server table to a SqlDataReader instance. The source data does not have to be located on SQL Server; you can use any data source that can be read to an IDataReader or loaded to a DataTable.


This sample will not run unless you have created the work tables as described in Bulk Copy Example Setup. This code is provided to demonstrate the syntax for using SqlBulkCopy only. If the source and destination tables are in the same SQL Server instance, it is easier and faster to use a Transact-SQL INSERT … SELECT statement to copy the data.

.NET Framework
Available since 2.0
Return to top
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
© 2015 Microsoft