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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With