发布网友 发布时间:2022-04-08 11:09
共1个回答
热心网友 时间:2022-04-08 12:39
AddRange是添加存储过程吗? public class DBHelper { //定义数据库链接属性 private static SqlConnection objSqlConnection; /// <summary> /// 获得数据库连接状态 /// </summary> public static SqlConnection ObjSqlConnection { get { string DBConnectionString = ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString; if (objSqlConnection == null) { objSqlConnection = new SqlConnection(DBConnectionString); objSqlConnection.Open(); } else if (objSqlConnection.State == System.Data.ConnectionState.Closed) { objSqlConnection.Open(); } else if (objSqlConnection.State == System.Data.ConnectionState.Broken) { objSqlConnection.Close(); objSqlConnection.Open(); } return objSqlConnection; } } public static void CloseConn() { if (objSqlConnection.State==ConnectionState.Open) { objSqlConnection.Close(); } } /// <summary> /// 无参存储过程执行增删改 /// </summary> /// <param name="cmdText">存储过程名</param> /// <returns>返回受影响行数</returns> public static int ExecuteNonQuery(string cmdText) { SqlCommand objSqlCommand = new SqlCommand(cmdText, ObjSqlConnection); objSqlCommand.CommandType = CommandType.StoredProcere; return objSqlCommand.ExecuteNonQuery(); } /// <summary> /// 带参存储过程执行增删改 /// </summary> /// <param name="cmdText">存储过程名</param> /// <param name="paras">参数</param> /// <returns>返回受影响行数</returns> public static int ExecuteNonQuery(string cmdText, params SqlParameter[] paras) { SqlCommand objSqlCommand = new SqlCommand(cmdText, ObjSqlConnection); objSqlCommand.CommandType = CommandType.StoredProcere; if (paras != null) { objSqlCommand.Parameters.AddRange(paras); } return objSqlCommand.ExecuteNonQuery(); } /// <summary> /// 无参存储过程查询数据的方法 /// </summary> /// <param name="cmdText">存储过程</param> /// <returns>返回数据集</returns> public static SqlDataReader GetReader(string cmdText) { SqlCommand objSqlCommand = new SqlCommand(cmdText, ObjSqlConnection); objSqlCommand.CommandType = CommandType.StoredProcere; return objSqlCommand.ExecuteReader(); } /// <summary> /// 带参存储过程查询数据方法 /// </summary> /// <param name="cmdText"></param> /// <param name="paras"></param> /// <returns></returns> public static SqlDataReader GetReader(string cmdText, params SqlParameter[] paras) { SqlCommand objSqlCommand = new SqlCommand(cmdText, ObjSqlConnection); objSqlCommand.CommandType = CommandType.StoredProcere; if (paras != null) { objSqlCommand.Parameters.AddRange(paras); } return objSqlCommand.ExecuteReader(); } public static SqlDataReader GetReaderBySql(string sql) { SqlCommand cmd = new SqlCommand(sql, ObjSqlConnection); SqlDataReader reader = cmd.ExecuteReader(); return reader; } /// <summary> /// 无参存储过程查询首行首列数据的方法 /// </summary> /// <param name="cmdText">存储过程名</param> /// <returns>返回首行首列的值</returns> public static object GetScaler(string cmdText) { SqlCommand objSqlCommand = new SqlCommand(cmdText, ObjSqlConnection); objSqlCommand.CommandType = CommandType.StoredProcere; return objSqlCommand.ExecuteScalar(); } /// <summary> /// 带参存储过程查询首行首列数据的方法 /// </summary> /// <param name="cmdText"></param> /// <param name="paras"></param> /// <returns></returns> public static object GetScalar(string cmdText, params SqlParameter[] paras) { SqlCommand objSqlCommand = new SqlCommand(cmdText, ObjSqlConnection); objSqlCommand.CommandType = CommandType.StoredProcere; objSqlCommand.Parameters.AddRange(paras); return objSqlCommand.ExecuteScalar(); } /// <summary> /// 查询返回DataTable /// </summary> /// <param name="cmdText">存储过程</param> /// <returns>返回DataTable</returns> public static DataTable GetDataSet(string cmdText) { DataSet ds = new DataSet(); SqlCommand objSqlCommand = new SqlCommand(cmdText, ObjSqlConnection); objSqlCommand.CommandType = CommandType.StoredProcere; SqlDataAdapter da = new SqlDataAdapter(objSqlCommand); da.Fill(ds); return ds.Tables[0]; } /// <summary> /// 查询返回DataTable /// </summary> /// <param name="cmdText">存储过程名字</param> /// <param name="paras">参数个数</param> /// <returns>返回DataTable</returns> public static DataTable GetDataSet(string cmdText, params SqlParameter[] paras) { DataSet ds = new DataSet(); SqlCommand objSqlCommand = new SqlCommand(cmdText, ObjSqlConnection); objSqlCommand.CommandType = CommandType.StoredProcere; objSqlCommand.Parameters.AddRange(paras); SqlDataAdapter da = new SqlDataAdapter(objSqlCommand); da.Fill(ds); return ds.Tables[0]; } }