Export (0) Print
Expand All

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)

'Declaration
Public Property BatchSize As Integer

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.

Important noteImportant

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.

Imports System.Data.SqlClient

Module Module1
    Sub Main()
        Dim connectionString As String = GetConnectionString()

        ' Open a connection to the AdventureWorks database. 
        Using sourceConnection As SqlConnection = _
           New SqlConnection(connectionString)
            sourceConnection.Open()

            ' Perform an initial count on the destination table. 
            Dim commandRowCount As New SqlCommand( _
            "SELECT COUNT(*) FROM dbo.BulkCopyDemoMatchingColumns;", _
                sourceConnection)
            Dim countStart As Long = _
               System.Convert.ToInt32(commandRowCount.ExecuteScalar())
            Console.WriteLine("Starting row count = {0}", countStart)

            ' Get data from the source table as a SqlDataReader. 
            Dim commandSourceData As SqlCommand = New SqlCommand( _
               "SELECT ProductID, Name, ProductNumber " & _
               "FROM Production.Product;", sourceConnection)
            Dim reader As SqlDataReader = commandSourceData.ExecuteReader

            ' Create the SqlBulkCopy object using a connection string.  
            ' In the real world you would not use SqlBulkCopy to move 
            ' data from one table to the other in the same database. 
            Using bulkCopy As SqlBulkCopy = _
              New SqlBulkCopy(connectionString)
                bulkCopy.DestinationTableName = "dbo.BulkCopyDemoMatchingColumns" 

                ' Set the BatchSize.
                bulkCopy.BatchSize = 50

                Try 
                    ' Write from the source to the destination.
                    bulkCopy.WriteToServer(reader)

                Catch ex As Exception
                    Console.WriteLine(ex.Message)

                Finally 
                    ' Close the SqlDataReader. The SqlBulkCopy 
                    ' object is automatically closed at the end 
                    ' of the Using block.
                    reader.Close()
                End Try 
            End Using 

            ' Perform a final count on the destination table 
            ' to see how many rows were added. 
            Dim countEnd As Long = _
                System.Convert.ToInt32(commandRowCount.ExecuteScalar())
            Console.WriteLine("Ending row count = {0}", countEnd)
            Console.WriteLine("{0} rows were added.", countEnd - countStart)

            Console.WriteLine("Press Enter to finish.")
            Console.ReadLine()
        End Using 
    End Sub 

    Private Function GetConnectionString() As String 
        ' To avoid storing the sourceConnection string in your code,  
        ' you can retrieve it from a configuration file.  
        Return "Data Source=(local);" & _
            "Integrated Security=true;" & _
            "Initial Catalog=AdventureWorks;" 
    End Function 
End Module

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft