using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Data; using System.Data.SqlClient; using System.Configuration; /// ///dbhelper 的摘要说明 /// /// namespace DBH { public class DBHelper { private static string conStr = ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString; private static SqlConnection _sqlCon; /// /// 连接对象 /// public static SqlConnection SqlCon { get { if (_sqlCon == null) { _sqlCon = new SqlConnection(); _sqlCon.ConnectionString = conStr; } if (_sqlCon.State == ConnectionState.Closed) //连接状态如果关闭 { _sqlCon.Open(); } return _sqlCon; } } /// /// 执行返回受影响函数的sql语句 /// /// sql语句 /// 连接对象 /// 返回第一行的第一列 public static int ExecuteNonQuery(string sql) { int result = 0; SqlCommand cmd = new SqlCommand(sql, SqlCon); result = cmd.ExecuteNonQuery(); SqlCon.Close(); return result; } /// /// 执行返回受影响函数的sql语句 /// /// sql语句 /// public static int ExecuteNonQuery(string sql,SqlParameter[] par) { int result = 0; SqlCommand cmd = new SqlCommand(sql, SqlCon); if (par != null) { cmd.Parameters.AddRange(par); } result = cmd.ExecuteNonQuery(); SqlCon.Close(); return result; } /// /// 执行返回受影响函数的sql语句 /// /// 带参数的sql语句或者存储过程名称 /// 参数 /// 连接对象 /// sql语句的类型(Text,StoredProcedure) /// 受影响的行数 public static int ExecuteNonQuery(string sql, SqlParameter[] sqlPar, CommandType type) { int result = 0; SqlCommand cmd = new SqlCommand(sql, SqlCon); cmd.CommandType = type; cmd.Parameters.AddRange(sqlPar); result = cmd.ExecuteNonQuery(); SqlCon.Close(); return result; } /// /// 执行返回第一行第一列的sql语句 /// /// 执行的sql语句 /// 查询的对象 public static object ExecuteScalar(string sql) { SqlCommand cmd = new SqlCommand(sql, SqlCon); object o = cmd.ExecuteScalar(); SqlCon.Close(); return o; } public static object ExecuteScalar(string sql, SqlParameter[] sqlPar, CommandType type) { SqlCommand cmd = new SqlCommand(sql, SqlCon); cmd.CommandType = type; if (sqlPar != null) cmd.Parameters.AddRange(sqlPar); object o = cmd.ExecuteScalar(); SqlCon.Close(); return o; } public static SqlDataReader ExecuteReader(string sql) { SqlCommand cmd = new SqlCommand(sql, SqlCon); //当sqlDatareader数据流关闭的时候,会自动关闭当前连接对象 return cmd.ExecuteReader(CommandBehavior.CloseConnection); } /// /// 执行并返回SqlDataReader类型数据集 /// /// /// /// /// public static SqlDataReader ExecuteReader(string sql, SqlParameter[] sqlPar, CommandType type) { SqlCommand cmd = new SqlCommand(sql, SqlCon); cmd.CommandType = type; if (sqlPar != null) cmd.Parameters.AddRange(sqlPar); //当sqlDatareader数据流关闭的时候,会自动关闭当前连接对象 return cmd.ExecuteReader(CommandBehavior.CloseConnection); } /// /// 获取数据集 /// /// 参数sql语句 /// DataTable类型的数据集 public static DataTable GetDataSet(string sql) { DataSet ds = new DataSet(); SqlDataAdapter da; using (SqlCommand cmd = new SqlCommand(sql, SqlCon)) { da = new SqlDataAdapter(cmd); da.Fill(ds); return ds.Tables[0]; } } public static DataSet GetDataSet2(string sql) { DataSet ds = new DataSet(); SqlDataAdapter da; using (SqlCommand cmd = new SqlCommand(sql, SqlCon)) { try { da = new SqlDataAdapter(cmd); da.Fill(ds); } catch { _sqlCon.Close(); } finally { _sqlCon.Close(); } return ds; } } /// /// /// /// sql语句 /// 参数 /// 类型(sql语句或存储过程) /// 返回DataTable类型的数据 public static DataTable GetDataSet(string sql, SqlParameter[] sqlPar, CommandType type) { DataSet ds = new DataSet(); SqlCommand cmd = new SqlCommand(sql, SqlCon); if (sqlPar != null) cmd.Parameters.AddRange(sqlPar); cmd.CommandType = type; SqlDataAdapter da = new SqlDataAdapter(cmd); da.Fill(ds); return ds.Tables[0]; } /// /// /// /// 参数 /// 返回执行语句 public static string ReturnStringScalar(string sql) { string result = ""; SqlCommand cmd = new SqlCommand(sql, SqlCon); try { result = cmd.ExecuteScalar().ToString(); } catch (Exception e) { result = "0"; } SqlCon.Close(); return result; } } }