Using SqlBulkCopy with System.Transactions namespace

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

The MSDN document
http://msdn2.microsoft.com/en-us/library/tchktcdk.aspx explicitely
states that it is possible to use the SqlBulkCopy class with a
transaction created using the classes in the
System.Transactions-namespace. However, I notice that the SqlBulkCopy
constructor only accepts an instance of a SqlTransaction-class and not
of any of the classes derived from System.Transactions.Transaction. So,
my question is this: how can I make sure that SqlBulkCopy makes use of
an existing transaction that I explicitely created using
CommittableTransaction?
 
SqlClient supports automatic transaction enlistment. You can start a new
System.Transactions transaction, then choose from the following:

1. Instantiate SqlBulkCopy with a connectionstring (automatic enlistment
occurs)
2. Insantiate a connection and pass it to SqlBulkCopy (again, automatic
enlistment)
3. If you have an existing connection already and wish to use it, call its
EnlistTransaction method before passing it to SqlBulkCopy
 
Hi Robert,

Thanks for your answer. I can't use automatic enlistment so I had already
enlisted my SqlConnection into a transaction using EnlistTransaction, but I
didn't think that was enough. If I understand your response correctly, than
that's all I need to enable transactions in SqlBulkCopy?

Michael
 
SqlBulkCopy needs a connection just like anything else ... so it seems
logical to me that if the underlying connection upon which SqlBulkCopy is
working is in a transaction scope, then everything it does will fall into
that scope.

Even if SqlBulkCopy only cloned the connection, or copied the
connectionstring or whatever, it still has to make a connection instance,
and as soon as it does, that connection would automatically enlist unless
the connectionstring specifically told it not to.

Robert
 
Back
Top