How to pass sql transaction to methods in different classes?
I'm working with ASP.NET(VB开发者_StackOverflow中文版.NET), SQL 2008 Project. I need to write data to more than one table with methods in different classes. I have to ensure all the data is written using one transaction so I'm using the older method (not a TransactionScope) So do i have to pass the connection & transaction both to the methods from main method (what if im using TransactionScope) ?
So do i have to pass the connection & transaction both to the methods from main method
Yes. Also, any commands that you create have to have both their connection and transaction properties assigned to the connection/transaction, respectively, that you've passed along.
(what if im using TransactionScope)
Infinitely easier. You can just open a new transaction scope in your main, and then any methods that need to be enlisted in the transaction scope can just open a new transaction scope with the option TransactionScopeOption.Required
and they will automatically enlist in the ambient transaction from main. You don't need to pass anything around.
精彩评论