C# 事务提交(非数据库)

.Net 2.0开始支持

static void Main(string[] args)
{
  using (TransactionScope ts = new TransactionScope())
  {
    UserBLL u = new UserBLL();
    TeacherBLL t = new TeacherBLL();
    u.ADD();
    t.ADD();
    ts.Complete();
  }
}

获取事务ID

Transaction.Current.TransactionInformation.LocalIdentifier

嵌套事务

static void Main(string[] args)
{
    using (TransactionScope ts = new TransactionScope())
    {
        Console.WriteLine(Transaction.Current.TransactionInformation.LocalIdentifier);
        UserBLL u = new UserBLL();
        TeacherBLL t = new TeacherBLL();
        u.ADD();
        using (TransactionScope ts2 = new TransactionScope(TransactionScopeOption.Required))
        {
            Console.WriteLine(Transaction.Current.TransactionInformation.LocalIdentifier);
            t.ADD();
            ts2.Complete();
        }
        ts.Complete();
        }
     }
}
1、使用嵌套事务时,默认
TransactionScopeOption的属性为Required

2、如果把TransactionScopeOption设为RequiresNew,则嵌套的事务块和外层的事务块各自独立,互不影响

3、
TransactionScopeOption设为Suppress则为取消当前区块的事务,一般很少使用。
原文地址:https://www.cnblogs.com/chengeng/p/3181170.html