using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.SqlClient; using System.Data; using System.Configuration; namespace SQLHelper { public static class SqlHelper { //从配置文件中获取连接字符串 readonly修饰的变量只能在初始化或构造函数中赋值;其他地方只能读取 //private static readonly string constr = ConfigurationManager.ConnectionStrings["OperationHistory"].ConnectionString; private static readonly string constr = "Data Source=/*主机名或者目标主机IP地址*/;Initial Catalog=/*数据库名*/;User=sa;PassWord=123"; /// <summary> /// 增、删、改的方法[ExecuteNonQuery] 返回所影响的行数,执行其他 /// </summary> /// <param name="sql">Sql命令</param> /// <param name="cmdtype">SQL语句(CommandType.Text)或者存储过程(CommandType.StoredProcedure)</param> /// <param name="pms"></param> /// <returns></returns> public static int ExecuteNonQuery(string sql, CommandType cmdtype, params SqlParameter[] pms) { int i = -1; try { using (SqlConnection con = new SqlConnection(constr)) { using (SqlCommand cmd = new SqlCommand(sql, con)) { //存储过程或者Sql语句 cmd.CommandType = cmdtype; if (pms != null) { cmd.Parameters.AddRange(pms); } con.Open(); i = cmd.ExecuteNonQuery(); } } } catch (Exception) { throw; } return i; } /// <summary> /// 执行查询、查询多少条数据;返回第一行,第一列方法[ExecuteScalar] 返回-1执行失败 /// </summary> /// <param name="sql">Sql命令</param> /// <param name="cmdtype">SQL语句(CommandType.Text)或者存储过程(CommandType.StoredProcedure)</param> /// <param name="pms"></param> /// <returns></returns> public static object ExecuteScalar(string sql, CommandType cmdtype, params SqlParameter[] pms) { try { using (SqlConnection con = new SqlConnection(constr)) { using (SqlCommand cmd = new SqlCommand(sql, con)) { //存储过程或者Sql语句 cmd.CommandType = cmdtype; if (pms != null) { cmd.Parameters.AddRange(pms); } con.Open(); return cmd.ExecuteScalar(); } } } catch (Exception) { throw; } } /// <summary> /// 执行查询,返回多行、多列的方法ExecuteReader() /// </summary> /// <param name="sql">Sql命令</param> /// <param name="cmdtype">SQL语句(CommandType.Text)或者存储过程(CommandType.StoredProcedure)</param> /// <param name="pms"></param> /// <returns></returns> public static SqlDataReader ExecuteReader(string sql, CommandType cmdtype, params SqlParameter[] pms) { SqlConnection con = new SqlConnection(constr); using (SqlCommand cmd = new SqlCommand(sql, con)) { //存储过程或者Sql语句 cmd.CommandType = cmdtype; if (pms != null) { cmd.Parameters.AddRange(pms); } try { con.Open(); return cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection); } catch { con.Close(); con.Dispose(); throw; } } } /// <summary> /// 执行返回多个查询时使用,返回DataTable类型 /// </summary> /// <param name="sql">Sql命令</param> /// <param name="cmdtype">SQL语句(CommandType.Text)或者存储过程(CommandType.StoredProcedure)</param> /// <param name="pms">参数</param> /// <returns></returns> public static DataTable ExecuteDataTable(string sql, CommandType cmdtype, params SqlParameter[] pms) { DataTable dt = new DataTable(); try { //通过adapter读取数据。 using (SqlDataAdapter adapter = new SqlDataAdapter(sql, constr)) { adapter.SelectCommand.CommandType = cmdtype; if (pms != null) { adapter.SelectCommand.Parameters.AddRange(pms); } adapter.Fill(dt); return dt; } } catch (Exception) { throw; } } /// <summary> /// 该方法是一个数据集方法在很多地方用得到的 /// </summary> /// <param name="sql">第一个参数是传sql语句</param> /// <param name="table">第二个参数是传数据库当中的表名</param> /// <returns></returns> public DataSet GetDataSet(string sql, CommandType cmdtype, string table) { try { using (SqlDataAdapter adapter = new SqlDataAdapter(sql, constr)) { adapter.SelectCommand.CommandType = cmdtype; DataSet ds = new DataSet(); adapter.Fill(ds, table); return ds; } } catch (Exception) { throw; } } } }