标签:sel sum type man out tor readers [] ++
public readonly string connStr = ConfigurationManager.ConnectionStrings["sql"].ConnectionString; /// <summary> /// 执行非插叙语句,返回受影响行数 /// </summary> /// <param name="cmdText">Sql语句</param> /// <param name="ps">参数列表</param> /// <returns>受影响行数</returns> public int ExecuteNonQuery(string cmdText, params SqlParameter[] ps) { return this.ExecuteNonQuery(cmdText, CommandType.Text, ps); } /// <summary> /// 执行存储过程 /// </summary> /// <param name="cmdText">存储过程名称</param> /// <param name="ps">参数列表</param> /// <returns>返回存储过程成功与否</returns> public int ExecuteNonQuerySP(string cmdText, params SqlParameter[] ps) { return this.ExecuteNonQuery(cmdText, CommandType.StoredProcedure, ps); } /// <summary> /// 执行SQL语句或存储过程,返回结果 /// </summary> /// <param name="cmdText">SQL语句或存储过程名称</param> /// <param name="type">执行类型</param> /// <param name="ps">参数列表</param> /// <returns>返回受影响行数或存储过程成功与否</returns> public int ExecuteNonQuery(string cmdText, CommandType type, params SqlParameter[] ps) { if (type == CommandType.Text) { try { using (SqlConnection conn = new SqlConnection(connStr)) { using (SqlCommand cmd = new SqlCommand(cmdText, conn)) { if (ps != null) { cmd.Parameters.AddRange(ps); } conn.Open(); return cmd.ExecuteNonQuery(); } } } catch (Exception ex) { throw ex; } } else if (type == CommandType.StoredProcedure) { try { ps[ps.Length - 1].Direction = ParameterDirection.Output; using (SqlConnection conn = new SqlConnection(connStr)) { using (SqlCommand cmd = new SqlCommand(cmdText, conn)) { cmd.CommandType = CommandType.StoredProcedure; if (ps != null) { cmd.Parameters.AddRange(ps); } conn.Open(); cmd.ExecuteNonQuery(); } } return (int)ps[ps.Length - 1].Value; } catch (Exception ex) { throw ex; } } else { return -1; } } /// <summary> /// 执行查询语句,返回第一行第一列数据 /// </summary> /// <param name="cmdText">Sql语句</param> /// <param name="ps">参数列表</param> /// <returns>第一行第一列数据</returns> public object ExecuteScalar(string cmdText, params SqlParameter[] ps) { return ExecuteScalar(cmdText, CommandType.Text, ps); } /// <summary> /// 执行存储过程,返回存储过程返回值 /// </summary> /// <param name="cmdText">存储过程名称</param> /// <param name="ps">参数列表</param> /// <returns>存储过程返回值</returns> public object ExecuteScalarSP(string cmdText, params SqlParameter[] ps) { return ExecuteScalar(cmdText, CommandType.StoredProcedure, ps); } /// <summary> /// 执行查询语句或存储过程,返回第一行第一列数据或存储过程返回值 /// </summary> /// <param name="cmdText">Sql语句或存储过程名称</param> /// <param name="ps">参数列表</param> /// <returns>第一行第一列数据或存储过程返回值</returns> public object ExecuteScalar(string cmdText, CommandType type, params SqlParameter[] ps) { if (type == CommandType.Text) { try { using (SqlConnection conn = new SqlConnection(connStr)) { using (SqlCommand cmd = new SqlCommand(cmdText, conn)) { if (ps != null) { cmd.Parameters.AddRange(ps); } conn.Open(); return cmd.ExecuteScalar(); } } } catch (Exception ex) { return ex; } } else if (type == CommandType.StoredProcedure) { try { for (int i = 0; i < ps.Length; i++) { if (ps[i].Value == null) { ps[i].Direction = ParameterDirection.Output; } } using (SqlConnection conn = new SqlConnection(connStr)) { using (SqlCommand cmd = new SqlCommand(cmdText, conn)) { cmd.CommandType = type; if (ps != null) { cmd.Parameters.AddRange(ps); } conn.Open(); cmd.ExecuteScalar(); } } List<Object> returnValue = new List<object>(); for (int i = 0; i < ps.Length; i++) { if (ps[i].Direction == ParameterDirection.Output) { returnValue.Add(ps[i].Value); } } return returnValue; } catch (Exception ex) { return ex; } } else { return -1; } } /// <summary> /// 执行查询语句,并返回一个DataReader阅读器 /// </summary> /// <param name="cmdText">Sql语句</param> /// <param name="ps">参数列表</param> /// <returns>DataReader阅读器</returns> public SqlDataReader ExecuteReader(string cmdText, params SqlParameter[] ps) { return ExecuteReader(cmdText, CommandType.Text, ps); } /// <summary> /// 执行存储过程,并返回一个DataReader阅读器 /// </summary> /// <param name="cmdText">存储过程名称</param> /// <param name="ps">参数列表</param> /// <returns>DataReader阅读器</returns> public SqlDataReader ExecuteReaderSP(string cmdText, params SqlParameter[] ps) { return ExecuteReader(cmdText, CommandType.StoredProcedure, ps); } /// <summary> /// 执行查询语句或存储过程,并返回一个DataReader阅读器 /// </summary> /// <param name="cmdText">Sql语句或存储过程名称</param> /// <param name="ps">参数列表</param> /// <returns>DataReader阅读器</returns> public SqlDataReader ExecuteReader(string cmdText, CommandType type, params SqlParameter[] ps) { SqlConnection conn = new SqlConnection(connStr); try { using (SqlCommand cmd = new SqlCommand(cmdText, conn)) { cmd.CommandType = type; if (ps != null) { cmd.Parameters.AddRange(ps); } conn.Open(); return cmd.ExecuteReader(CommandBehavior.CloseConnection); } } catch (Exception ex) { conn.Dispose(); throw (ex); } } /// <summary> /// 执行查询语句,返回DataSet /// </summary> /// <param name="cmdText">Sql语句</param> /// <param name="ps">参数列表</param> /// <returns>返回的DataSet</returns> public DataSet GetDataSet(string cmdText, params SqlParameter[] ps) { return this.GetDataSet(cmdText, CommandType.Text, ps); } /// <summary> /// 执行存储过程返回DataSet /// </summary> /// <param name="cmdText">存储过程名</param> /// <param name="ps">参数列表</param> /// <returns>返回DataSet</returns> public DataSet GetDataSetSP(string cmdText, params SqlParameter[] ps) { return this.GetDataSet(cmdText, CommandType.StoredProcedure, ps); } /// <summary> /// 执行存储过程,或SQL语句,返回DataSet /// </summary> /// <param name="cmdText">SQL语句或存储过程名称</param> /// <param name="type">执行类型</param> /// <param name="ps">参数列表</param> /// <returns>返回DataSet</returns> public DataSet GetDataSet(string cmdText, CommandType type, params SqlParameter[] ps) { DataSet ds = new DataSet(); try { using (SqlDataAdapter sda = new SqlDataAdapter(cmdText, connStr)) { if (ps != null) { sda.SelectCommand.Parameters.AddRange(ps); } sda.SelectCommand.CommandType = CommandType.StoredProcedure; sda.Fill(ds); } return ds; } catch (Exception ex) { ds.Dispose(); throw (ex); } }
这个SqlHelper里面用了很多重载,体验会好一点。并且ExecuteScalar里面支持多返回值的存储过程,会以一个List<Object>的形式返回。
标签:sel sum type man out tor readers [] ++
原文地址:http://www.cnblogs.com/xueyeduling/p/6158793.html