SQLBulkCopy used in conjunction with Transaction and firing an event each time a batch is copied

c# sqlbulkcopy sql-server

Question

Im currently uploading data to MS SQL server via SQLBulkCopy and Transactions. I would like to be able to raise an event after each batch has been uploaded (I have already tried SQLRowsCopied event and it doesnt work, see quote below)

MSDN quote:

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.

http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlbulkcopy.sqlrowscopied(v=vs.80).aspx

So i basically cant have my cake and eat it :( Does anyone know a solution around this as i would like to fire an event after each batch has been uploaded.

Thanks for your help.

Accepted Answer

I have finally decided to use an internal transaction. ATM it seems to be working fine if an error is encountered the current batch is rolled back. Regarding Ben's comment above, i tried this but its not possible, its not as though it loops through a section of code where you could raise an event. In the end i used the notifyafter property, which raises an event after x rows have been copied, to raise my event. Probably not the best way of doing it but i couldnt figure out a better way. Hope someone finds this useful.




Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why