Export (0) Print
Expand All

SqlBulkCopy::WriteToServer Method (DataTable, DataRowState)

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.

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

public:
void WriteToServer(
	DataTable^ table, 
	DataRowState rowState
)

Parameters

table
Type: System.Data::DataTable

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

rowState
Type: System.Data::DataRowState

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

Only rows in the DataTable that are in the states indicated in the rowState argument and have not been deleted are copied to the destination table.

NoteNote

If Deleted is specified, any Unchanged, Added, and Modified rows will also be copied to the server. No exception will be raised.

While the bulk copy operation is in progress, the associated destination SqlConnection is busy serving it, and no other operations can be performed on the connection.

The ColumnMappings collection maps from the DataTable columns to the destination database table.

The following Console application demonstrates how to bulk load only the rows in a DataTable that match a specified state. In this case, only unchanged rows are added. The destination table is a table in the AdventureWorks database.

In this example, a DataTable is created at run time and three rows are added to it. Before the WriteToServer method is executed, one of the rows is edited. The WriteToServer method is called with a DataRowState.Unchanged rowState argument, so only the two unchanged rows are bulk copied to the destination.

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.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