public static class SqlHelper { //连接字符串 private static readonly string connStr = ConfigurationManager.ConnectionStrings["mssql"].ConnectionString; //1.执行增、删、改的方法:ExecuteNonQuery public static int ExecuteNonQuery(string sql, params SqlParameter[] pms) { using (SqlConnection con = new SqlConnection(connStr)) { using (SqlCommand cmd = new SqlCommand(sql, con)) { if (pms != null) { cmd.Parameters.AddRange(pms); } con.Open(); return cmd.ExecuteNonQuery(); } } } //2.封装一个执行返回单个对象的方法:ExecuteScalar() public static object ExecuteScalar(string sql, params SqlParameter[] pms) { using (SqlConnection con = new SqlConnection(connStr)) { using (SqlCommand cmd = new SqlCommand(sql, con)) { if (pms != null) { cmd.Parameters.AddRange(pms); } con.Open(); return cmd.ExecuteScalar(); } } } //3.执行查询多行多列的数据的方法:ExecuteReader public static SqlDataReader ExecuteReader(string sql, params SqlParameter[] pms) { SqlConnection con = new SqlConnection(connStr); using (SqlCommand cmd = new SqlCommand(sql, con)) { if (pms != null) { cmd.Parameters.AddRange(pms); } try { con.Open(); return cmd.ExecuteReader(CommandBehavior.CloseConnection); } catch (Exception) { con.Close(); con.Dispose(); throw; } } } //4.执行返回DataTable的方法 public static DataTable ExecuteDataTable(string sql, params SqlParameter[] pms) { DataTable dt = new DataTable(); using (SqlDataAdapter adapter = new SqlDataAdapter(sql, connStr)) { if (pms != null) { adapter.SelectCommand.Parameters.AddRange(pms); } adapter.Fill(dt); } return dt; } }
【注意】添加全局配置文件:
<connectionStrings > <add name ="mssql" connectionString="Server=localhost;DataBase=Login;Uid=sa;Pwd=123456" providerName="System.Data.SqlClient"/> </connectionStrings>
解释版:
SqlHepler类代码如下:
public static class SqlHelper { /// <summary> /// 获取连接字符串 /// </summary> /// <returns>连接字符串</returns> public static string GetSqlConnectionString() { return ConfigurationManager.ConnectionStrings["Sql"].ConnectionString; } /// <summary> /// 封装一个执行的sql 返回受影响的行数 /// </summary> /// <param name="sqlText">执行的sql脚本</param> /// <param name="parameters">参数集合</param> /// <returns>受影响的行数</returns> public static int ExecuteNonQuery(string sqlText, params SqlParameter[] parameters) { using (SqlConnection conn = new SqlConnection(GetSqlConnectionString())) { using (SqlCommand cmd = conn.CreateCommand()) { conn.Open(); cmd.CommandText = sqlText; cmd.Parameters.AddRange(parameters); return cmd.ExecuteNonQuery(); } } } /// <summary> /// 执行sql,返回查询结果中的第一行第一列的值 /// </summary> /// <param name="sqlText">执行的sql脚本</param> /// <param name="parameters">参数集合</param> /// <returns>查询结果中的第一行第一列的值</returns> public static object ExecuteScalar(string sqlText, params SqlParameter[] parameters) { using (SqlConnection conn = new SqlConnection(GetSqlConnectionString())) { using (SqlCommand cmd = conn.CreateCommand()) { conn.Open(); cmd.CommandText = sqlText; cmd.Parameters.AddRange(parameters); return cmd.ExecuteScalar(); } } } /// <summary> /// 执行sql 返回一个DataTable /// </summary> /// <param name="sqlText">执行的sql脚本</param> /// <param name="parameters">参数集合</param> /// <returns>返回一个DataTable</returns> public static DataTable ExecuteDataTable(string sqlText, params SqlParameter[] parameters) { using (SqlDataAdapter adapter = new SqlDataAdapter(sqlText, GetSqlConnectionString())) { DataTable dt = new DataTable(); adapter.SelectCommand.Parameters.AddRange(parameters); adapter.Fill(dt); return dt; } } /// <summary> /// 执行sql脚本 /// </summary> /// <param name="sqlText">执行的sql脚本</param> /// <param name="parameters">参数集合</param> /// <returns>返回一个SqlDataReader</returns> public static SqlDataReader ExecuteReader(string sqlText, params SqlParameter[] parameters) { //SqlDataReader要求,它读取数据的时候有,它独占它的SqlConnection对象,而且SqlConnection必须是Open状态 SqlConnection conn = new SqlConnection(GetSqlConnectionString());//不要释放连接,因为后面还需要连接打开状态 SqlCommand cmd = conn.CreateCommand(); conn.Open(); cmd.CommandText = sqlText; cmd.Parameters.AddRange(parameters); //CommandBehavior.CloseConnection当SqlDataReader释放的时候,顺便把SqlConnection对象也释放掉 return cmd.ExecuteReader(CommandBehavior.CloseConnection); } }
配置连接字符串:
<connectionStrings> <add name = "Sql" connectionString = "server=数据库地址;uid=用户名;pwd=密码;database=数据库名" providerName = "System.Data.SqlClient" / > < / connectionStrings>