Yup. Look into TransactionScope : http://msdn.microsoft.com/
It could be as simple as:
using(TransactionScope scope = new TransactionScope())
{
// perform transactions here using SqlConnection,SqlCommand as you normally do
// the Transaction will be handled for you
scope.Complete(); // commit
}
if there is an error the complete will not happen and since TransactionScope is IDisposable and used inside a using block, it will rollback. See the link given above for more info.
Main Topics
Browse All Topics





by: maligerPosted on 2008-06-25 at 05:12:26ID: 21864599
So you want update 2 different databases? If so, you want non-trivial thing, SqlTranstaction is above 1 specific database. Transaction Servers (Enterprise transactions).
look into
TransactionScope
and "Implementing an Implicit Transaction using Transaction Scope" MSDN article
You can create 2nd transaction on 2nd databae _before_ you commit 1st transaction and commit/rollback both transactions together later. This is somewhat lightweighted solution, but can be usable in your case (perhaps). Try get the operations in transaction as fast as possible, since every delay makes heavy load on both databases involved.