引入命名空间
using System.Transactions ;
TransactionScope 包裹代码块
using(transactionscope scope=new transactionscope())
{
// to do somethig
scope.Comlete()
}
调用scope.Comlete(),事务完成,提交操作。如果没有调用Comlete(),则自动回滚。
注意的问题
要确保对支持事务的资源的登记放在此范围内
using(var scope=new Transactionscope())
{
// 要确保对支持事务的资源的登记放在此范围内,如数据库资源的打开
A a=new A();
scope.Comlete()
}
如果 a 在Transactionscope 事务范围外,
则无法保证事务的执行