SqlBulkCopy.SqlRowsCopied Event
Assembly: System.Data (in system.data.dll)
'Declaration Public Event SqlRowsCopied As SqlRowsCopiedEventHandler 'Usage Dim instance As SqlBulkCopy Dim handler As SqlRowsCopiedEventHandler AddHandler instance.SqlRowsCopied, handler
/** @event */ public void add_SqlRowsCopied (SqlRowsCopiedEventHandler value) /** @event */ public void remove_SqlRowsCopied (SqlRowsCopiedEventHandler value)
JScript supports the use of events, but not the declaration of new ones.
Note that the settings of NotifyAfter and BatchSize are independent. Receipt of a SqlRowsCopied event does not imply that any rows have been sent to the server or committed.
The Close method cannot be called from this event. Doing this will cause an InvalidOperationException being thrown, and the SqlBulkCopy object state will not change. If the user wants to cancel the operation from the event, the Abort property of the SqlRowsCopiedEventArgs can be used. (See Performing a Bulk Copy Operation in a Transaction for examples that use the Abort property.)
No action, such as transaction activity, is supported in the connection during the execution of the bulk copy operation, and it is recommended that you not use the same connection used during the SqlRowsCopied event. However, you can open a different connection.
The following console application demonstrates how to bulk load data using a connection that is already open. The NotifyAfter property is set so that the event handler is called after every 50 rows copied to the table.
In this example, the connection is first used to read data from a SQL Server table to a SqlDataReader instance. Note that the source data does not have to be located on SQL Server; you can use any data source that can be read to an IDataReader or loaded to a DataTable.
Important: |
|---|
| This sample will not run unless you have created the work tables as described in Creating Tables for the Bulk Copy Examples. 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) sourceConnection.Open() ' Perform an initial count on the destination table. Dim commandRowCount As New SqlCommand( _ "SELECT COUNT(*) FROM dbo.BulkCopyDemoMatchingColumns;", _ sourceConnection) Dim countStart As Long = _ System.Convert.ToInt32(commandRowCount.ExecuteScalar()) Console.WriteLine("NotifyAfter Sample") 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 ' Create the SqlBulkCopy object using a connection string. ' In the real world you would not use SqlBulkCopy to move ' data from one table to the other in the same database. Using bulkCopy As SqlBulkCopy = _ New SqlBulkCopy(connectionString) bulkCopy.DestinationTableName = "dbo.BulkCopyDemoMatchingColumns" ' Set up the event handler to notify after 50 rows. AddHandler bulkCopy.SqlRowsCopied, AddressOf OnSqlRowsCopied bulkCopy.DestinationTableName = _ "dbo.BulkCopyDemoMatchingColumns" bulkCopy.NotifyAfter = 50 Try ' Write from the source to the destination. bulkCopy.WriteToServer(reader) Catch ex As Exception Console.WriteLine(ex.Message) Finally ' Close the SqlDataReader. The SqlBulkCopy ' object is automatically closed at the end ' of the Using block. reader.Close() End Try End Using ' Perform a final count on the destination table ' to see how many rows were added. Dim countEnd As Long = _ System.Convert.ToInt32(commandRowCount.ExecuteScalar()) Console.WriteLine("Ending row count = {0", countEnd) Console.WriteLine("{0 rows were added.", countEnd - countStart) Console.WriteLine("Press Enter to finish.") Console.ReadLine() End Using End Sub Private Sub OnSqlRowsCopied(ByVal sender As Object, _ ByVal args As SqlRowsCopiedEventArgs) Console.WriteLine("Copied {0 so far...", args.RowsCopied) 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
Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.
Important: