SqlBulkCopy Constructor (String^, SqlBulkCopyOptions)

 

Initializes and opens a new instance of SqlConnection based on the supplied connectionString. The constructor uses that SqlConnection to initialize a new instance of the SqlBulkCopy class. The SqlConnection instance behaves according to options supplied in the copyOptions parameter.

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

public:
SqlBulkCopy(
	String^ connectionString,
	SqlBulkCopyOptions copyOptions
)

Parameters

connectionString
Type: System::String^

The string defining the connection that will be opened for use by the SqlBulkCopy instance. If your connection string does not use Integrated Security = true, you can use SqlBulkCopy or SqlBulkCopy and SqlCredential to pass the user ID and password more securely than by specifying the user ID and password as text in the connection string.

copyOptions
Type: System.Data.SqlClient::SqlBulkCopyOptions

A combination of values from the SqlBulkCopyOptions enumeration that determines which data source rows are copied to the destination table.

You can obtain detailed information about all the bulk copy options in the SqlBulkCopyOptions topic.

The following console application demonstrates how to perform a bulk load by using a connection specified as a string. An option is set to use the value in the identity column of the source table when you load the destination table. In this example, the source data is first read from a SQL Server table to a SqlDataReader instance. The source table and destination table each include an Identity column. By default, a new value for the Identity column is generated in the destination table for each row added. In this example, an option is set when the connection is opened that forces the bulk load process to use the Identity values from the source table instead. To see how the option changes the way the bulk load works, run the sample with the dbo.BulkCopyDemoMatchingColumns table empty. All rows load from the source. Then run the sample again without emptying the table. An exception is thrown and the code writes a message to the console notifying you that rows weren't added because of primary key constraint violations.

System_CAPS_importantImportant

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.

No code example is currently available or this language may not be supported.

.NET Framework
Available since 2.0
Return to top
Show: