标签:style blog io ar color os sp on div
1 public class MSSqlHelper : IDisposable 2 { 3 private static readonly string connstr = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString; 4 5 private SqlConnection _con; 6 public void Close() 7 { 8 if (_con.State == System.Data.ConnectionState.Open || _con.State == System.Data.ConnectionState.Broken) 9 { 10 _con.Close(); 11 } 12 } 13 public void Open() 14 { 15 if (this._con == null || this._con.State == System.Data.ConnectionState.Closed) 16 { 17 this._con.Open(); 18 } 19 else if (this._con.State == System.Data.ConnectionState.Broken) 20 { 21 this._con.Close(); 22 this._con.Open(); 23 } 24 } 25 public MSSqlHelper() 26 { 27 _con = new SqlConnection(connstr); 28 this.Open(); 29 } 30 public MSSqlHelper(string connStr) 31 { 32 _con = new SqlConnection(connStr); 33 this.Open(); 34 } 35 36 #region 37 public int ExecuteNonQuery(string sql, params SqlParameter[] parameters) 38 { 39 SqlCommand cmd = new SqlCommand(); 40 PrepareCommand(cmd, CommandType.Text, sql, parameters); 41 return cmd.ExecuteNonQuery(); 42 } 43 44 public object ExecuteScalar(string sql, params SqlParameter[] parameters) 45 { 46 SqlCommand cmd = new SqlCommand(); 47 PrepareCommand(cmd, CommandType.Text, sql, parameters); 48 return cmd.ExecuteScalar(); 49 } 50 51 public DataTable ExecuteDataTable(string sql, params SqlParameter[] parameters) 52 { 53 DataTable dt = new DataTable(); 54 SqlCommand cmd = new SqlCommand(); 55 PrepareCommand(cmd, CommandType.Text, sql, parameters); 56 SqlDataAdapter da = new SqlDataAdapter(cmd); 57 da.Fill(dt); 58 return dt; 59 } 60 61 public SqlDataReader GetSqlDataReader(string sql, params SqlParameter[] parameters) 62 { 63 SqlCommand cmd = new SqlCommand(); 64 PrepareCommand(cmd, CommandType.Text, sql, parameters); 65 return cmd.ExecuteReader(); 66 } 67 68 private void PrepareCommand(SqlCommand cmd, CommandType commandType, string commandtext, params SqlParameter[] parameters) 69 { 70 cmd.CommandType = commandType; 71 cmd.CommandText = commandtext; 72 cmd.Connection = this._con; 73 cmd.Transaction = this._tran; 74 if (parameters != null) 75 { 76 cmd.Parameters.Clear(); 77 cmd.Parameters.AddRange(parameters); 78 } 79 } 80 #endregion 81 82 #region 事务 83 84 private SqlTransaction _tran; 85 /// <summary> 86 /// 开启事务 87 /// </summary> 88 public void BeginTran() 89 { 90 _tran = this._con.BeginTransaction(); 91 } 92 /// <summary> 93 /// 提交事务 94 /// </summary> 95 public void CommitTran() 96 { 97 this._tran.Commit(); 98 } 99 /// <summary> 100 /// 回滚事务 101 /// </summary> 102 public void RollbackTran() 103 { 104 this._tran.Rollback(); 105 } 106 #endregion 107 108 public void Dispose() 109 { 110 this.Close(); 111 } 112 }
//调用
string sql = @"INSERT INTO dbo.Person( Name, Age, gender, Tel )VALUES(‘lk‘,‘12‘,‘0‘,‘1111222‘)"; string sql2 = @"INSERT INTO dbo.Person( Name, Age, gender, Tel )VALUES(‘xl‘,‘12‘,‘1‘,‘1111222‘)"; using (MSSqlHelper db = new MSSqlHelper()) { try { db.BeginTran(); db.ExecuteNonQuery(sql); db.ExecuteNonQuery(sql2); db.CommitTran(); } catch { db.RollbackTran(); } }
标签:style blog io ar color os sp on div
原文地址:http://www.cnblogs.com/5tomorrow/p/4160971.html