Export (0) Print
Expand All

SqlBulkCopy::Close Method

Closes the SqlBulkCopy instance.

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

public:
void Close()

After you call a Close on the SqlBulkCopy object, no other operation will succeed. Calls to the WriteToServer method will throw an InvalidOperationException.

Calling the Close method from the SqlRowsCopied event causes an InvalidOperationException to be thrown.

Note that open SqlBulkCopy instances are closed implicitly at the end of a using block.

The following example uses the same SqlBulkCopy instance to add sales orders and their associated details to two destination tables. Because the AdventureWorks sales order tables are large, the sample reads only orders placed by a certain account number and bulk copies those orders and details to the destination tables. The Close method is used only after both bulk copy operations are complete.

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.

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

.NET Framework

Supported in: 4.6, 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