Same Transaction, multiple methods. How to do it?

  • Thread starter Thread starter Frankie
  • Start date Start date
F

Frankie

In a C# Windows Forms application, using the SqlClient data provider...

I have methodA() and methodB().

methodA starts a transaction on its own command and conection objects, does
bunch of stuff, then either commits or rollsback the transaction.

methodA calls methodB many times.

methodB creates its own connection and command objects and does a bunch of
other stuff.

How can I get the command and connection objects of methodB to participate
in the transaction in methodA?

Thanks!
 
I've not tried this, but maybe you'll have to pass your transaction object
to Method B as a parameter, and enlist method B's stuff in the transaction.
You'll have to use the same connection being used in Method A as well, I'd
think.

Hope this helps,

Anne
 
You can just pass the transaction class which provides a reference to the connection.

public void MethodB(SqlTransaction tran)
{
if (tran == null)
// create new connection
else
// use existing conn (tran.Connection)
}
 
Back
Top