using System; using System.Collections.Generic; using System.Configuration; using System.Data; using System.Data.SqlClient; using System.Linq; using System.Text; using System.Threading.Tasks; namespace DAL { public class SqlHelper { private SqlConnection con = null;//定义一些下面要用到的数据 private SqlCommand cmd = null; private SqlDataReader sdr = null; /// <summary> /// 构造函数,初始化用 /// </summary> /// public SqlHelper() { string str = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString; //连接字符串 con = new SqlConnection(str); } /// <summary> /// 获取数据库的链接 /// </summary> /// <returns>打开之后返回去</returns> private SqlConnection GetCon() { if (con.State == ConnectionState.Closed)//判断下con的状态 { con.Open();//判断一下,如果是关闭的话,就打开 } return con; } /// <summary> /// 执行不带参数的增删改SQL语句或存储过程 /// </summary> /// <param name="sql">增删改SQL语句或存储过程</param> /// <param name="ct">命令类型</param> /// <returns>返回受影响的行</returns> public int ExecuteNonQuery(string sql, CommandType ct) { int res;//用来存储返回的行数 try { cmd.CommandType = ct;//给command类型定义 cmd = new SqlCommand(sql, GetCon());//执行cmd,数据库执行语句,调用GetCon函数,判断下数据库是否open res = cmd.ExecuteNonQuery(); } catch (Exception ex) { throw ex; } finally { if (con.State == ConnectionState.Open)//最后用完需要关闭一下数据库,这里我们用try catch来操作,其实可以不用这么麻烦的,我们可以用using,这里只是写给广大读者看下 { con.Close(); } } return res; } /// <summary> /// 执行带参数的sql增删改语句/查询过程 /// </summary> /// <param name="sql">sql增删改语句</param> /// <param name="ps">参数集合</param> /// <param name="ct">存储过程名字,类型</param> /// <returns>返回受影响的行</returns> public int ExecuteNonQuery(string sql, SqlParameter[] ps, CommandType ct) { int res; using (cmd = new SqlCommand(sql, GetCon())) { cmd.CommandType = ct; cmd.Parameters.AddRange(ps); res = cmd.ExecuteNonQuery(); } return res; } /// <summary> /// 该方法可以执行sql查询和存储过程查询 /// </summary> /// <param name="sql">传入得sql语句</param> /// <param name="ct">存储过程名字</param> /// <returns>返回一张表</returns> public DataTable ExecuteQuery(string sql,CommandType ct) { DataTable dt = new DataTable(); cmd = new SqlCommand(sql, GetCon()); cmd.CommandType = ct; using ( sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection))//因为加了参数了,所以sdr关闭的时候con也随着关闭,多看看 { dt.Load(sdr); } return dt; } /// <summary> /// 执行带参数的sql查询语句 /// </summary> /// <param name="sql">sql查询语句</param> /// <param name="ps">参数集合</param> /// <returns></returns> public DataTable ExecuteQuery(string sql, SqlParameter[] ps, CommandType ct) { DataTable dt = new DataTable(); cmd = new SqlCommand(sql, GetCon()); cmd.CommandType=ct; cmd.Parameters.AddRange(ps); using (sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection))//因为加了参数了,所以sdr关闭的时候con也随着关闭,多看看 { dt.Load(sdr); } return dt; } /// <summary> /// 返回一行一列的值 /// </summary> /// <param name="sql"></param> /// <param name="ct"></param> /// <returns></returns> public string ExecuteScalar(string sql, CommandType ct)/这个主要用来操作分页的时候获取总页数的操作 { cmd = new SqlCommand(sql, GetCon()); object obj = cmd.ExecuteScalar(); using (sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection))//因为加了参数了,所以sdr关闭的时候con也随着关闭,多看看 { if (obj != null) { return obj.ToString(); } } return ""; } } }
注意:CommandBehavior.CloseConnection其用在ExecuteReader(c)中,因为其返回对象前不能关闭数据库连接,须用CommandBehavior.CloseConnection;
它能够保证当SqlDataReader对象被关闭时,其依赖的连接也会被自动关闭。