Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does SqlBulkCopy Enlist in Ambient Transaction?

I can't seem to find a definitive answer on this. I have a SqlBulkCopy operation that is wrapped in a TransactionScope with a number of other operations.

I am aware of overloads in the SqlBulkCopy constructor that allow passing a SqlTransaction object. Assuming I am not passing a transaction in the constructor, will the command automatically participate in the ambient transaction created by the TransactionScope?

like image 840
Phil Sandler Avatar asked Jul 21 '15 18:07

Phil Sandler


1 Answers

I've done some testing, and it appears that SqlBulkCopy does, in fact, honor an ambient transaction, at least in .Net 4.5.

To test, I did a SqlBulkCopy operation while debugging, and verified that the rows made it to the database (via a NOLOCK/dirty read query). I then threw an exception on the next line of code, and allowed the TransactionScope to roll back. I then verified that the rows were no longer in the database.

like image 143
Phil Sandler Avatar answered Oct 13 '22 08:10

Phil Sandler