/// <summary> /// 开始事务 /// </summary> public static IDbConnection BeginTransaction(string connectionString) { IDbConn = DataBaseFactory.GetIDbConnection(connectionString); IDbConn.Open(); IDbComm = DataBaseFactory.GetIDbCommand(IDbConn); IDbTrans = IDbConn.BeginTransaction(IsolationLevel.ReadCommitted); IDbComm.Connection = IDbConn; IDbComm.Transaction = IDbTrans; return(IDbConn); }
/// <summary> /// 通过事务执行多条SQL语句 /// </summary> /// <param name="sqlList">SQL语句集</param> /// <returns></returns> public static DataResult ExecuteByTransaction(List <string> sqlList) { DataResult result = new DataResult(); using (IDbConn = DataBaseFactory.GetIDbConnection()) { IDbConn.Open(); IDbComm = DataBaseFactory.GetIDbCommand(); IDbComm.Connection = IDbConn; IDbTrans = IDbConn.BeginTransaction(IsolationLevel.ReadCommitted);// DataBaseFactory.GetIDbTransaction(); if (IDbTrans != null) { IDbComm.Transaction = IDbTrans; // IDbTrans = IDbConn.BeginTransaction(IsolationLevel.ReadCommitted); } try { for (int i = 0; i < sqlList.Count; i++) { string str = sqlList[i].ToString(); if (str.Trim().Length > 1) { IDbComm.CommandText = str; result.RecordCount += IDbComm.ExecuteNonQuery(); } } IDbTrans.Commit(); return(result); } catch (Exception exception) { IDbTrans.Rollback(); IDbConn.Close(); result.Error = exception.Message; return(result); } finally { IDbConn.Close(); } } }
/// <summary> /// 通过事务执行多条SQL语句 /// </summary> /// <param name="sqlList">SQL语句集</param> /// <returns></returns> public bool ExecuteByTransaction(List <string> sqlList) { using (IDbConn = DataBaseFactory.GetIDbConnection()) { IDbConn.Open(); IDbComm = DataBaseFactory.GetIDbCommand(); IDbComm.Connection = IDbConn; IDbTrans = IDbConn.BeginTransaction();// DataBaseFactory.GetIDbTransaction(); if (IDbTrans != null) { IDbComm.Transaction = IDbTrans; IDbTrans = IDbConn.BeginTransaction(IsolationLevel.ReadCommitted); } try { for (int i = 0; i < sqlList.Count; i++) { string str = sqlList[i].ToString(); if (str.Trim().Length > 1) { IDbComm.CommandText = str; IDbComm.ExecuteNonQuery(); } } IDbTrans.Commit(); return(true); } catch (Exception exception) { IDbTrans.Rollback(); IDbConn.Close(); return(false); throw new Exception(exception.Message); } finally { IDbConn.Close(); } } }
/// <summary> /// 得到开一个开始事务 /// </summary> /// <returns></returns> public static IDbTransaction GetIDbTransaction() { IDbConn = GetIDbConnection(); IDbTrans = IDbConn.BeginTransaction(); return(IDbTrans); }