Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLBulkCopy Row Count When Complete

Tags:

I am using SQLBulkCopy to move large amounts of data. I implemented the notification event to notify me every time a certain number of rows have been processed, but the OnSqlRowsCopied event does not fire when the job is completed. How do I get the total number of rows copied when the SQLBulkCopy writetoserver completes?

like image 326
SchwartzE Avatar asked Jul 27 '09 14:07

SchwartzE


1 Answers

The following hack (using reflection) is an option:

    /// <summary>     /// Helper class to process the SqlBulkCopy class     /// </summary>     static class SqlBulkCopyHelper     {         static FieldInfo rowsCopiedField = null;          /// <summary>         /// Gets the rows copied from the specified SqlBulkCopy object         /// </summary>         /// <param name="bulkCopy">The bulk copy.</param>         /// <returns></returns>         public static int GetRowsCopied(SqlBulkCopy bulkCopy)         {             if (rowsCopiedField == null)             {                 rowsCopiedField = typeof(SqlBulkCopy).GetField("_rowsCopied", BindingFlags.NonPublic | BindingFlags.GetField | BindingFlags.Instance);             }              return (int)rowsCopiedField.GetValue(bulkCopy);         }     } 

And then use the class as follows:

int rowsCopied = SqlBulkCopyHelper.GetRowsCopied(bulkCopyObjectInYourCode); 

Hope this helps.

like image 186
Benzi Avatar answered Sep 21 '22 13:09

Benzi