This documentation is archived and is not being maintained.

SqlBulkCopy Class

Lets you efficiently bulk load a SQL Server table with data from another source.

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

Public NotInheritable Class SqlBulkCopy _
	Implements IDisposable
Dim instance As SqlBulkCopy

Microsoft SQL Server includes a popular command-prompt utility named bcp for moving data from one table to another, whether on a single server or between servers. The SqlBulkCopy class lets you write managed code solutions that provide similar functionality. There are other ways to load data into a SQL Server table (INSERT statements, for example), but SqlBulkCopy offers a significant performance advantage over them.

The SqlBulkCopy class can be used to write data only to SQL Server tables. However, the data source is not limited to SQL Server; any data source can be used, as long as the data can be loaded to a DataTable instance or read with a IDataReader instance.

SqlBulkCopy will fail when bulk loading a DataTable column of type SqlDateTime into a SQL Server column whose type is one of the date/time types added in SQL Server 2008.

The following console application demonstrates how to load data using the SqlBulkCopy class. In this example, a SqlDataReader is used to copy data from the Production.Product table in the SQL Server 2005 AdventureWorks database to a similar table in the same database.

Important noteImportant Note:

This sample will not run unless you have created the work tables as described in Bulk Copy Example Setup (ADO.NET). 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)

            ' Perform an initial count on the destination table. 
            Dim commandRowCount As New SqlCommand( _
            "SELECT COUNT(*) FROM dbo.BulkCopyDemoMatchingColumns;", _
            Dim countStart As Long = _
            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

            ' Open the destination connection. In the real world you would  
            ' not use SqlBulkCopy to move data from one table to the other    
            ' in the same database. This is for demonstration purposes only. 
            Using destinationConnection As SqlConnection = _
                New SqlConnection(connectionString)

                ' Set up the bulk copy object.  
                ' The column positions in the source data reader  
                ' match the column positions in the destination table,  
                ' so there is no need to map columns. 
                Using bulkCopy As SqlBulkCopy = _
                  New SqlBulkCopy(destinationConnection)
                    bulkCopy.DestinationTableName = _

                        ' Write from the source to the destination.

                    Catch ex As Exception

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

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

                Console.WriteLine("Press Enter to finish.")
            End Using 
        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


Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

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

.NET Framework

Supported in: 3.5, 3.0, 2.0