SqlBulkCopy.WriteToServerAsync Method

Definition

Important

Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.

An asynchronous version of WriteToServer, which copies all rows from a data source to a destination table specified by the DestinationTableName property of the SqlBulkCopy object.

Overloads

WriteToServerAsync(IDataReader, CancellationToken)

The asynchronous version of WriteToServer(IDataReader), which copies all rows in the supplied IDataReader to a destination table specified by the DestinationTableName property of the SqlBulkCopy object.

The cancellation token can be used to request that the operation be abandoned before the command timeout elapses. Exceptions will be reported via the returned Task object.

WriteToServerAsync(DbDataReader)

The asynchronous version of WriteToServer(DbDataReader), which copies all rows from the supplied DbDataReader array to a destination table specified by the DestinationTableName property of the SqlBulkCopy object.

WriteToServerAsync(DataRow[])

The asynchronous version of WriteToServer(DataRow[]), which copies all rows from the supplied DataRow array to a destination table specified by the DestinationTableName property of the SqlBulkCopy object.

WriteToServerAsync(DataTable)

The asynchronous version of WriteToServer(DataTable), which copies all rows in the supplied DataTable to a destination table specified by the DestinationTableName property of the SqlBulkCopy object.

WriteToServerAsync(IDataReader)

The asynchronous version of WriteToServer(IDataReader), which copies all rows in the supplied IDataReader to a destination table specified by the DestinationTableName property of the SqlBulkCopy object.

WriteToServerAsync(DbDataReader, CancellationToken)

The asynchronous version of WriteToServer(DbDataReader), which copies all rows from the supplied DbDataReader array to a destination table specified by the DestinationTableName property of the SqlBulkCopy object.

WriteToServerAsync(DataRow[], CancellationToken)

The asynchronous version of WriteToServer(DataRow[]), which copies all rows from the supplied DataRow array to a destination table specified by the DestinationTableName property of the SqlBulkCopy object.

The cancellation token can be used to request that the operation be abandoned before the command timeout elapses. Exceptions will be reported via the returned Task object.

WriteToServerAsync(DataTable, DataRowState)

The asynchronous version of WriteToServer(DataTable, DataRowState), which copies only rows that match the supplied row state in the supplied DataTable to a destination table specified by the DestinationTableName property of the SqlBulkCopy object.

WriteToServerAsync(DataTable, CancellationToken)

The asynchronous version of WriteToServer(DataTable), which copies all rows in the supplied DataTable to a destination table specified by the DestinationTableName property of the SqlBulkCopy object.

The cancellation token can be used to request that the operation be abandoned before the command timeout elapses. Exceptions will be reported via the returned Task object.

WriteToServerAsync(DataTable, DataRowState, CancellationToken)

The asynchronous version of WriteToServer(DataTable, DataRowState), which copies only rows that match the supplied row state in the supplied DataTable to a destination table specified by the DestinationTableName property of the SqlBulkCopy object.

The cancellation token can be used to request that the operation be abandoned before the command timeout elapses. Exceptions will be reported via the returned Task object.

Remarks

If multiple active result sets (MARS) is disabled, WriteToServer makes the connection busy. If MARS is enabled, you can interleave calls to WriteToServer with other commands in the same connection.

The number of rows that are rolled back when one fails depends on several things:

When there is an error while sending data to the server, the current batch (as specified by BatchSize) will be rolled back. If UseInternalTransaction is not specified and you have your own transaction, the entire transaction will be rolled back (which includes all previous batches as well).

Use SqlRowsCopied to know how many rows were copied to the server.

WriteToServerAsync(IDataReader, CancellationToken)

Source:
System.Data.SqlClient.notsupported.cs

The asynchronous version of WriteToServer(IDataReader), which copies all rows in the supplied IDataReader to a destination table specified by the DestinationTableName property of the SqlBulkCopy object.

The cancellation token can be used to request that the operation be abandoned before the command timeout elapses. Exceptions will be reported via the returned Task object.

C#
public System.Threading.Tasks.Task WriteToServerAsync(System.Data.IDataReader reader, System.Threading.CancellationToken cancellationToken);

Parameters

reader
IDataReader

A IDataReader whose rows will be copied to the destination table.

cancellationToken
CancellationToken

The cancellation instruction. A None value in this parameter makes this method equivalent to WriteToServerAsync(DataTable).

Returns

A task representing the asynchronous operation.

Exceptions

Calling WriteToServerAsync(IDataReader) multiple times for the same instance before task completion.

Calling WriteToServerAsync(IDataReader) and WriteToServer(IDataReader) for the same instance before task completion.

The connection drops or is closed during WriteToServerAsync(IDataReader) execution.

Returned in the task object, the SqlBulkCopy object was closed during the method execution.

Returned in the task object, there was a connection pool timeout.

Returned in the task object, the SqlConnection object is closed before method execution.

The IDataReader was closed before the completed Task returned.

The IDataReader's associated connection was closed before the completed Task returned.

Context Connection=true is specified in the connection string.

Returned in the task object, any error returned by SQL Server that occurred while opening the connection.

The cancellation token was canceled. This exception is stored into the returned task.

Remarks

For more information about asynchronous programming in the .NET Framework Data Provider for SQL Server, see Asynchronous Programming.

This method stores in the task it returns all non-usage exceptions that the method's synchronous counterpart can throw. If an exception is stored into the returned task, that exception will be thrown when the task is awaited. Usage exceptions, such as ArgumentException, are still thrown synchronously. For the stored exceptions, see the exceptions thrown by WriteToServer(IDataReader).

Applies to

.NET 10 (package-provided) and other versions
Product Versions
.NET 6 (package-provided), 7 (package-provided), 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)

WriteToServerAsync(DbDataReader)

Source:
System.Data.SqlClient.notsupported.cs

The asynchronous version of WriteToServer(DbDataReader), which copies all rows from the supplied DbDataReader array to a destination table specified by the DestinationTableName property of the SqlBulkCopy object.

C#
public System.Threading.Tasks.Task WriteToServerAsync(System.Data.Common.DbDataReader reader);

Parameters

reader
DbDataReader

A DbDataReader whose rows will be copied to the destination table.

Returns

A task representing the asynchronous operation.

Remarks

For more information about asynchronous programming in the .NET Framework Data Provider for SQL Server, see Asynchronous Programming.

This method stores in the task it returns all non-usage exceptions that the method's synchronous counterpart can throw. If an exception is stored into the returned task, that exception will be thrown when the task is awaited. Usage exceptions, such as ArgumentException, are still thrown synchronously. For the stored exceptions, see the exceptions thrown by WriteToServer(DbDataReader).

Applies to

.NET 10 (package-provided) and other versions
Product Versions
.NET Core 1.0, Core 1.1, 6 (package-provided), 7 (package-provided), 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)

WriteToServerAsync(DataRow[])

Source:
System.Data.SqlClient.notsupported.cs

The asynchronous version of WriteToServer(DataRow[]), which copies all rows from the supplied DataRow array to a destination table specified by the DestinationTableName property of the SqlBulkCopy object.

C#
public System.Threading.Tasks.Task WriteToServerAsync(System.Data.DataRow[] rows);

Parameters

rows
DataRow[]

An array of DataRow objects that will be copied to the destination table.

Returns

A task representing the asynchronous operation.

Exceptions

Calling WriteToServerAsync(DataRow[]) multiple times for the same instance before task completion.

Calling WriteToServerAsync(DataRow[]) and WriteToServer(DataRow[]) for the same instance before task completion.

The connection drops or is closed during WriteToServerAsync(DataRow[]) execution.

Returned in the task object, the SqlBulkCopy object was closed during the method execution.

Returned in the task object, there was a connection pool timeout.

Returned in the task object, the SqlConnection object is closed before method execution.

Context Connection=true is specified in the connection string.

Returned in the task object, any error returned by SQL Server that occurred while opening the connection.

Remarks

For more information about asynchronous programming in the .NET Framework Data Provider for SQL Server, see Asynchronous Programming.

This method stores in the task it returns all non-usage exceptions that the method's synchronous counterpart can throw. If an exception is stored into the returned task, that exception will be thrown when the task is awaited. Usage exceptions, such as ArgumentException, are still thrown synchronously. For the stored exceptions, see the exceptions thrown by WriteToServer(DataRow[]).

Applies to

.NET 10 (package-provided) and other versions
Product Versions
.NET 6 (package-provided), 7 (package-provided), 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)

WriteToServerAsync(DataTable)

Source:
System.Data.SqlClient.notsupported.cs

The asynchronous version of WriteToServer(DataTable), which copies all rows in the supplied DataTable to a destination table specified by the DestinationTableName property of the SqlBulkCopy object.

C#
public System.Threading.Tasks.Task WriteToServerAsync(System.Data.DataTable table);

Parameters

table
DataTable

A DataTable whose rows will be copied to the destination table.

Returns

A task representing the asynchronous operation.

Exceptions

Calling WriteToServerAsync(DataTable) multiple times for the same instance before task completion.

Calling WriteToServerAsync(DataTable) and WriteToServer(DataTable) for the same instance before task completion.

The connection drops or is closed during WriteToServerAsync(DataTable) execution.

Returned in the task object, the SqlBulkCopy object was closed during the method execution.

Returned in the task object, there was a connection pool timeout.

Returned in the task object, the SqlConnection object is closed before method execution.

Context Connection=true is specified in the connection string.

Returned in the task object, any error returned by SQL Server that occurred while opening the connection.

Remarks

For more information about asynchronous programming in the .NET Framework Data Provider for SQL Server, see Asynchronous Programming.

This method stores in the task it returns all non-usage exceptions that the method's synchronous counterpart can throw. If an exception is stored into the returned task, that exception will be thrown when the task is awaited. Usage exceptions, such as ArgumentException, are still thrown synchronously. For the stored exceptions, see the exceptions thrown by WriteToServer(DataTable).

Applies to

.NET 10 (package-provided) and other versions
Product Versions
.NET 6 (package-provided), 7 (package-provided), 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)

WriteToServerAsync(IDataReader)

Source:
System.Data.SqlClient.notsupported.cs

The asynchronous version of WriteToServer(IDataReader), which copies all rows in the supplied IDataReader to a destination table specified by the DestinationTableName property of the SqlBulkCopy object.

C#
public System.Threading.Tasks.Task WriteToServerAsync(System.Data.IDataReader reader);

Parameters

reader
IDataReader

A IDataReader whose rows will be copied to the destination table.

Returns

A task representing the asynchronous operation.

Exceptions

Calling WriteToServerAsync(IDataReader) multiple times for the same instance before task completion.

Calling WriteToServerAsync(IDataReader) and WriteToServer(IDataReader) for the same instance before task completion.

The connection drops or is closed during WriteToServerAsync(IDataReader) execution.

Returned in the task object, the SqlBulkCopy object was closed during the method execution.

Returned in the task object, there was a connection pool timeout.

Returned in the task object, the SqlConnection object is closed before method execution.

The IDataReader was closed before the completed Task returned.

The IDataReader's associated connection was closed before the completed Task returned.

Context Connection=true is specified in the connection string.

Returned in the task object, any error returned by SQL Server that occurred while opening the connection.

Remarks

For more information about asynchronous programming in the .NET Framework Data Provider for SQL Server, see Asynchronous Programming.

This method stores in the task it returns all non-usage exceptions that the method's synchronous counterpart can throw. If an exception is stored into the returned task, that exception will be thrown when the task is awaited. Usage exceptions, such as ArgumentException, are still thrown synchronously. For the stored exceptions, see the exceptions thrown by WriteToServer(IDataReader).

Applies to

.NET 10 (package-provided) and other versions
Product Versions
.NET 6 (package-provided), 7 (package-provided), 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)

WriteToServerAsync(DbDataReader, CancellationToken)

Source:
System.Data.SqlClient.notsupported.cs

The asynchronous version of WriteToServer(DbDataReader), which copies all rows from the supplied DbDataReader array to a destination table specified by the DestinationTableName property of the SqlBulkCopy object.

C#
public System.Threading.Tasks.Task WriteToServerAsync(System.Data.Common.DbDataReader reader, System.Threading.CancellationToken cancellationToken);

Parameters

reader
DbDataReader

A DbDataReader whose rows will be copied to the destination table.

cancellationToken
CancellationToken

The cancellation instruction. A None value in this parameter makes this method equivalent to WriteToServerAsync(DbDataReader).

Returns

Returns Task.

Exceptions

The cancellation token was canceled. This exception is stored into the returned task.

Remarks

This method stores in the task it returns all non-usage exceptions that the method's synchronous counterpart can throw. If an exception is stored into the returned task, that exception will be thrown when the task is awaited. Usage exceptions, such as ArgumentException, are still thrown synchronously. For the stored exceptions, see the exceptions thrown by WriteToServer(DbDataReader).

Applies to

.NET 10 (package-provided) and other versions
Product Versions
.NET Core 1.0, Core 1.1, 6 (package-provided), 7 (package-provided), 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)

WriteToServerAsync(DataRow[], CancellationToken)

Source:
System.Data.SqlClient.notsupported.cs

The asynchronous version of WriteToServer(DataRow[]), which copies all rows from the supplied DataRow array to a destination table specified by the DestinationTableName property of the SqlBulkCopy object.

The cancellation token can be used to request that the operation be abandoned before the command timeout elapses. Exceptions will be reported via the returned Task object.

C#
public System.Threading.Tasks.Task WriteToServerAsync(System.Data.DataRow[] rows, System.Threading.CancellationToken cancellationToken);

Parameters

rows
DataRow[]

An array of DataRow objects that will be copied to the destination table.

cancellationToken
CancellationToken

The cancellation instruction. A None value in this parameter makes this method equivalent to WriteToServerAsync(DataTable).

Returns

A task representing the asynchronous operation.

Exceptions

Calling WriteToServerAsync(DataRow[]) multiple times for the same instance before task completion.

Calling WriteToServerAsync(DataRow[]) and WriteToServer(DataRow[]) for the same instance before task completion.

The connection drops or is closed during WriteToServerAsync(DataRow[]) execution.

Returned in the task object, the SqlBulkCopy object was closed during the method execution.

Returned in the task object, there was a connection pool timeout.

Returned in the task object, the SqlConnection object is closed before method execution.

Context Connection=true is specified in the connection string.

Returned in the task object, any error returned by SQL Server that occurred while opening the connection.

The cancellation token was canceled. This exception is stored into the returned task.

Remarks

For more information about asynchronous programming in the .NET Framework Data Provider for SQL Server, see Asynchronous Programming.

This method stores in the task it returns all non-usage exceptions that the method's synchronous counterpart can throw. If an exception is stored into the returned task, that exception will be thrown when the task is awaited. Usage exceptions, such as ArgumentException, are still thrown synchronously. For the stored exceptions, see the exceptions thrown by WriteToServer(DataRow[]).

Applies to

.NET 10 (package-provided) and other versions
Product Versions
.NET 6 (package-provided), 7 (package-provided), 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)

WriteToServerAsync(DataTable, DataRowState)

Source:
System.Data.SqlClient.notsupported.cs

The asynchronous version of WriteToServer(DataTable, DataRowState), which copies only rows that match the supplied row state in the supplied DataTable to a destination table specified by the DestinationTableName property of the SqlBulkCopy object.

C#
public System.Threading.Tasks.Task WriteToServerAsync(System.Data.DataTable table, System.Data.DataRowState rowState);

Parameters

table
DataTable

A DataTable whose rows will be copied to the destination table.

rowState
DataRowState

A value from the DataRowState enumeration. Only rows matching the row state are copied to the destination.

Returns

A task representing the asynchronous operation.

Exceptions

Calling WriteToServerAsync(DataTable, DataRowState) multiple times for the same instance before task completion.

Calling WriteToServerAsync(DataTable, DataRowState) and WriteToServer(DataTable, DataRowState) for the same instance before task completion.

The connection drops or is closed during WriteToServerAsync(DataTable, DataRowState) execution.

Returned in the task object, the SqlBulkCopy object was closed during the method execution.

Returned in the task object, there was a connection pool timeout.

Returned in the task object, the SqlConnection object is closed before method execution.

Context Connection=true is specified in the connection string.

Returned in the task object, any error returned by SQL Server that occurred while opening the connection.

Remarks

For more information about asynchronous programming in the .NET Framework Data Provider for SQL Server, see Asynchronous Programming.

This method stores in the task it returns all non-usage exceptions that the method's synchronous counterpart can throw. If an exception is stored into the returned task, that exception will be thrown when the task is awaited. Usage exceptions, such as ArgumentException, are still thrown synchronously. For the stored exceptions, see the exceptions thrown by WriteToServer(DataTable, DataRowState).

Applies to

.NET 10 (package-provided) and other versions
Product Versions
.NET 6 (package-provided), 7 (package-provided), 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)

WriteToServerAsync(DataTable, CancellationToken)

Source:
System.Data.SqlClient.notsupported.cs

The asynchronous version of WriteToServer(DataTable), which copies all rows in the supplied DataTable to a destination table specified by the DestinationTableName property of the SqlBulkCopy object.

The cancellation token can be used to request that the operation be abandoned before the command timeout elapses. Exceptions will be reported via the returned Task object.

C#
public System.Threading.Tasks.Task WriteToServerAsync(System.Data.DataTable table, System.Threading.CancellationToken cancellationToken);

Parameters

table
DataTable

A DataTable whose rows will be copied to the destination table.

cancellationToken
CancellationToken

The cancellation instruction. A None value in this parameter makes this method equivalent to WriteToServerAsync(DataTable).

Returns

A task representing the asynchronous operation.

Exceptions

Calling WriteToServerAsync(DataTable) multiple times for the same instance before task completion.

Calling WriteToServerAsync(DataTable) and WriteToServer(DataTable) for the same instance before task completion.

The connection drops or is closed during WriteToServerAsync(DataTable) execution.

Returned in the task object, the SqlBulkCopy object was closed during the method execution.

Returned in the task object, there was a connection pool timeout.

Returned in the task object, the SqlConnection object is closed before method execution.

Context Connection=true is specified in the connection string.

Returned in the task object, any error returned by SQL Server that occurred while opening the connection.

The cancellation token was canceled. This exception is stored into the returned task.

Remarks

For more information about asynchronous programming in the .NET Framework Data Provider for SQL Server, see Asynchronous Programming.

This method stores in the task it returns all non-usage exceptions that the method's synchronous counterpart can throw. If an exception is stored into the returned task, that exception will be thrown when the task is awaited. Usage exceptions, such as ArgumentException, are still thrown synchronously. For the stored exceptions, see the exceptions thrown by WriteToServer(DataTable).

Applies to

.NET 10 (package-provided) and other versions
Product Versions
.NET 6 (package-provided), 7 (package-provided), 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)

WriteToServerAsync(DataTable, DataRowState, CancellationToken)

Source:
System.Data.SqlClient.notsupported.cs

The asynchronous version of WriteToServer(DataTable, DataRowState), which copies only rows that match the supplied row state in the supplied DataTable to a destination table specified by the DestinationTableName property of the SqlBulkCopy object.

The cancellation token can be used to request that the operation be abandoned before the command timeout elapses. Exceptions will be reported via the returned Task object.

C#
public System.Threading.Tasks.Task WriteToServerAsync(System.Data.DataTable table, System.Data.DataRowState rowState, System.Threading.CancellationToken cancellationToken);

Parameters

table
DataTable

A DataTable whose rows will be copied to the destination table.

rowState
DataRowState

A value from the DataRowState enumeration. Only rows matching the row state are copied to the destination.

cancellationToken
CancellationToken

The cancellation instruction. A None value in this parameter makes this method equivalent to WriteToServerAsync(DataTable).

Returns

A task representing the asynchronous operation.

Exceptions

Calling WriteToServerAsync(DataTable, DataRowState) multiple times for the same instance before task completion.

Calling WriteToServerAsync(DataTable, DataRowState) and WriteToServer(DataTable, DataRowState) for the same instance before task completion.

The connection drops or is closed during WriteToServerAsync(DataTable, DataRowState) execution.

Returned in the task object, the SqlBulkCopy object was closed during the method execution.

Returned in the task object, there was a connection pool timeout.

Returned in the task object, the SqlConnection object is closed before method execution.

Context Connection=true is specified in the connection string.

Returned in the task object, any error returned by SQL Server that occurred while opening the connection.

The cancellation token was canceled. This exception is stored into the returned task.

Remarks

For more information about asynchronous programming in the .NET Framework Data Provider for SQL Server, see Asynchronous Programming.

This method stores in the task it returns all non-usage exceptions that the method's synchronous counterpart can throw. If an exception is stored into the returned task, that exception will be thrown when the task is awaited. Usage exceptions, such as ArgumentException, are still thrown synchronously. For the stored exceptions, see the exceptions thrown by WriteToServer(DataTable, DataRowState).

Applies to

.NET 10 (package-provided) and other versions
Product Versions
.NET 6 (package-provided), 7 (package-provided), 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)