using System; using System.Collections.Generic; //using System.Linq; using System.Text; using System.Data; using System.Data.SqlClient; using System.Data.Common; using System.Configuration; using System.Collections; using Common; namespace Data.DBHelper { /// /// public class TransactionManager { private SqlTransaction _trans;//事务变量 private SqlConnection _conn;//数据连接 // private string strConn = string.Empty;//链接字符串 private static string strConn = string.Empty;//链接字符串 #region 公共属性 /// /// 获得事务实体 /// public SqlTransaction Trans { get { return _trans; } } #endregion 公共属性 #region 公共方法 /// /// 构造函数,初始化连接字符串和数据库连接。 /// public TransactionManager() { //strConn = SqlHelper.GetConnection(); if (strConn == null || strConn == "") { strConn = ConfigurationManager.ConnectionStrings["SqlConnectionString"].ToString(); if (ConfigurationManager.AppSettings["enableEncryptConnectionString"] == "1") { strConn = SecurityUtil.DecryptDES(strConn); } } _conn = new SqlConnection(strConn); } /// /// 开始事务,默认的事务锁定行为。 /// public void BeginTransaction() { BeginTransaction(IsolationLevel.ReadCommitted); } /// /// 开始事务。如果读取时要锁定行,请用枚举:IsolationLevel.Serializable。 /// /// 事务锁定行为 public void BeginTransaction(IsolationLevel isolationLevel) { if (_conn.State != ConnectionState.Open) { _conn.Open(); } try { _trans = _conn.BeginTransaction(isolationLevel); } catch { _conn.Close(); _trans.Dispose(); } } /// /// 提交事务 /// public void Commit() { try { _trans.Commit(); } finally { _conn.Close(); _trans.Dispose(); } } /// /// 回滚事务 /// public void Rollback() { try { _trans.Rollback(); } finally { _conn.Close(); _trans.Dispose(); } } #endregion 公共方法 }//类结束 }//命名空间结束