/// <summary> /// 使用TransactionScope封装事务[基本方法] /// </summary> /// <param name="method"></param> /// <param name="error"></param> /// <returns></returns> public bool PackageTrans(TransMethod method, out string error) { error = ""; using (var trans = new TransactionScope()) { CallContext.SetData(Base.UseTransactionScopeName, true); try { var a = method(out error); if (!a) { CallContext.SetData(Base.UseTransactionScopeName, false); return(false); } trans.Complete(); } catch (Exception ero) { CallContext.SetData(Base.UseTransactionScopeName, false); error = "提交事务时发生错误:" + ero.Message; return(false); } } CallContext.SetData(Base.UseTransactionScopeName, false); return(true); }
bool PackageTrans(DbContextInner dbContext, TransMethod method, out string error, System.Data.IsolationLevel isolationLevel = System.Data.IsolationLevel.ReadCommitted) { error = ""; var db = DBExtendFactory.CreateDBExtend(dbContext); db.BeginTran(isolationLevel); bool result; try { result = method(out error); if (!result) { db.RollbackTran(); return(false); } db.CommitTran(); } catch (Exception ero) { error = "提交事务时发生错误:" + ero.Message; db.RollbackTran(); return(false); } return(result); }
/// <summary> /// 使用DbTransaction封装事务,不能跨库 /// 请将数据访问对象写在方法体内 /// </summary> /// <param name="method"></param> /// <param name="error"></param> /// <returns></returns> public bool PackageTrans2(TransMethod method, out string error) { error = ""; CallContext.SetData("_BeginTransContext", true); var db = GetDBExtend(true); db.BeginTran(); try { var a = method(out error); if (!a) { db.RollbackTran(); CallContext.SetData("_BeginTransContext", false); return(false); } db.CommitTran(); } catch (Exception ero) { error = "提交事务时发生错误:" + ero.Message; db.RollbackTran(); CallContext.SetData("_BeginTransContext", false); return(false); } CallContext.SetData("_BeginTransContext", false); return(true); }
// zaridi transformaci polygonu pri cteni public BoxTrans(IPolygonReader polygon, TransMethod transfuncs) { if (polygon == null) { throw (new ArgumentNullException("BoxTrans(polygon,..)")); } if (transfuncs == null) { throw (new ArgumentNullException("BoxTrans(..,transfuncs)")); } myPoly = polygon; d_Trans = transfuncs; }
/// <summary> /// 使用DbTransaction封装事务,不能跨库 /// 请将数据访问对象写在方法体内 /// 可嵌套调用 /// </summary> /// <param name="method"></param> /// <param name="error"></param> /// <returns></returns> public bool PackageTrans2(TransMethod method, out string error) { error = ""; var _useCRLContext = CallContext.GetData <bool>(Base.UseCRLContextFlagName);//事务已开启,内部事务不用处理 using (var context = new CRLDbConnectionScope()) { var db = GetDBExtend(); if (!_useCRLContext) { db.BeginTran(); } bool result; try { result = method(out error); if (!_useCRLContext) { if (!result) { db.RollbackTran(); CallContext.SetData(Base.UseCRLContextFlagName, false); return(false); } db.CommitTran(); } } catch (Exception ero) { error = "提交事务时发生错误:" + ero.Message; if (!_useCRLContext) { db.RollbackTran(); CallContext.SetData(Base.UseCRLContextFlagName, false); } return(false); } if (!_useCRLContext) { CallContext.SetData(Base.UseCRLContextFlagName, false); } return(result); } }
/// <summary> /// 使用TransactionScope封装事务[基本方法] /// </summary> /// <param name="method"></param> /// <param name="error"></param> /// <returns></returns> public bool PackageTrans(TransMethod method, out string error) { error = ""; using (var trans = new TransactionScope()) { try { var a = method(out error); if (!a) { return(false); } trans.Complete(); } catch (Exception ero) { error = "提交事务时发生错误:" + ero.Message; return(false); } } return(true); }
/// <summary> /// 使用DbTransaction封装事务(不推荐) /// </summary> /// <param name="db"></param> /// <param name="method"></param> /// <param name="error"></param> /// <returns></returns> public bool PackageTrans2(AbsDBExtend db, TransMethod method, out string error) { error = ""; db.BeginTran(); try { var a = method(out error); if (!a) { db.RollbackTran(); return(false); } db.CommitTran(); } catch (Exception ero) { error = "提交事务时发生错误:" + ero.Message; db.RollbackTran(); return(false); } return(true); }