Pankaj Agarwal
Pankaj Agarwal

Reputation: 11311

How to maintain transactions in two different databases

I have to maintain transactions between two different databases. I need to rollback if there is any error occurred in Database1 then all changes in database2 should be rollback.

I have two connection string in my web.config file.

Upvotes: 1

Views: 870

Answers (2)

mika
mika

Reputation: 6962

The answer depends on if you need distributed transactions between two database server instances, or transactions between two databases in a single instance. In the first case you'll need a transaction manager like MSDTC, but in the second case the database server should be able to do the job by itself.

TransactionScope will escalate the transaction to MSDTC if necessary. The rules for this are somewhat subtle. If the two databases are on a single SQL Server 2008 instance, you shouldn't need MSDTC.

See also:

Upvotes: 2

Darin Dimitrov
Darin Dimitrov

Reputation: 1038780

You could use the TransactionScope class:

using(var tx = new TransactionScope())
{
    // TODO: your DB queries here
    tx.Complete();
}

Upvotes: 1

Related Questions