using (TransactionScope ts = TransactionHelper.GetTransactionScope()) { try { ... ts.Complete(); ... } catch (Exception ex) { ... } }
事务帮助类
/// <summary> /// 事务帮助类 /// </summary> public class TransactionHelper { /// <summary> /// 获取TransactionScope,设置级别 /// </summary> /// <param name="LationLevel">事务隔离级别</param> /// <param name="TimeOut">超时时间</param> /// <param name="ScopeOption">事务范围</param> /// <returns></returns> public static TransactionScope GetTransactionScope(IsolationLevel LationLevel = IsolationLevel.ReadUncommitted, int TimeOut = 90, TransactionScopeOption ScopeOption = TransactionScopeOption.Required) { TransactionOptions transactionOption = new TransactionOptions(); //设置事务隔离级别 transactionOption.IsolationLevel = LationLevel; // 设置事务超时时间为90秒 transactionOption.Timeout = new TimeSpan(0, 0, TimeOut); return new TransactionScope(ScopeOption, transactionOption); } }