简体   繁体   中英

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.

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:

You could use the TransactionScope class:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM