2

I use 2 different dlls to connect to 2 database in my project . each dll has it's own DAL layer .

now in a page I need to insert a set of related data into these 2 databases . I want to use transaction to make sure data are inserted correctly . but as I just have access to public methods of Insert() of each assembly , not the ADO.net codes . how can I handle this situation ? is there anything like this in c# ?

beginTransaction(){
  dll1.class.Insert(data);
  dll2.className.Insert(relatedData);
  ....
}
4

1 回答 1

5

You are looking for the TransactionScope class:

Makes a code block transactional.

Example usage:

using (var tx = new TransactionScope())
{
  dll1.class.Insert(data);
  dll2.className.Insert(relatedData);

  tx.Complete(); // if not executed, transaction will rollback
}

As @Anders Abel commented, the servers running the databases involved in the transaction will need the MSDTC service to be running and configured correctly on both the DB servers and the client machines. The DTCPing utility is very helpful in this regard.

As Steve B commented, there is an assumption here that all the databases and the associated drivers support distributed transaction enlistment. Check your documentation...

于 2012-07-29T19:54:25.950 回答