public class SQLHelper
{
//连接数据库
static string connStr = ConfigurationManager.ConnectionStrings["sql"].ConnectionString;
/// <summary>
/// 执行非查询语句
/// </summary>
/// <param name="sql">sql语句</param>
/// <param name="ps">参数</param>
/// <returns>受影响行数</returns>
private static int ExecuteNonQuery(string sql, params SqlParameter[] ps)
{
using (SqlConnection conn=new SqlConnection (connStr))
{
using (SqlCommand cmd=new SqlCommand(sql,conn))
{
cmd.Parameters.AddRange(ps);
conn.Open();
return cmd.ExecuteNonQuery();
}
}
}
/// <summary>
/// 执行单数据的查询
/// </summary>
/// <param name="sql">sql语句</param>
/// <param name="ps">参数</param>
/// <returns>第一行第一列的值</returns>
private static object ExecuteScalar(string sql, params SqlParameter[] ps)
{
using (SqlConnection conn = new SqlConnection(connStr))
{
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
cmd.Parameters.AddRange(ps);
conn.Open();
return cmd.ExecuteScalar();
}
}
}
/// <summary>
/// 查询,读取数据
/// </summary>
/// <param name="sql"></param>
/// <param name="ps"></param>
/// <returns>返回SqlDataReader类型</returns>
private static SqlDataReader ExecuteReader(string sql, params SqlParameter[] ps)
{
SqlConnection conn = new SqlConnection(connStr);
try
{
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
cmd.Parameters.AddRange(ps);
conn.Open();
return cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
}
}
catch (Exception ex)
{
conn.Dispose();
throw ex;
}
}
private static DataSet GetDataSet(string sql, params SqlParameter[] ps)
{
DataSet ds = new DataSet();
using (SqlDataAdapter sda = new SqlDataAdapter(sql, connStr))
{
sda.SelectCommand.Parameters.AddRange(ps);
sda.Fill(ds);
}
return ds;
}
}
{
//连接数据库
static string connStr = ConfigurationManager.ConnectionStrings["sql"].ConnectionString;
/// <summary>
/// 执行非查询语句
/// </summary>
/// <param name="sql">sql语句</param>
/// <param name="ps">参数</param>
/// <returns>受影响行数</returns>
private static int ExecuteNonQuery(string sql, params SqlParameter[] ps)
{
using (SqlConnection conn=new SqlConnection (connStr))
{
using (SqlCommand cmd=new SqlCommand(sql,conn))
{
cmd.Parameters.AddRange(ps);
conn.Open();
return cmd.ExecuteNonQuery();
}
}
}
/// <summary>
/// 执行单数据的查询
/// </summary>
/// <param name="sql">sql语句</param>
/// <param name="ps">参数</param>
/// <returns>第一行第一列的值</returns>
private static object ExecuteScalar(string sql, params SqlParameter[] ps)
{
using (SqlConnection conn = new SqlConnection(connStr))
{
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
cmd.Parameters.AddRange(ps);
conn.Open();
return cmd.ExecuteScalar();
}
}
}
/// <summary>
/// 查询,读取数据
/// </summary>
/// <param name="sql"></param>
/// <param name="ps"></param>
/// <returns>返回SqlDataReader类型</returns>
private static SqlDataReader ExecuteReader(string sql, params SqlParameter[] ps)
{
SqlConnection conn = new SqlConnection(connStr);
try
{
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
cmd.Parameters.AddRange(ps);
conn.Open();
return cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
}
}
catch (Exception ex)
{
conn.Dispose();
throw ex;
}
}
private static DataSet GetDataSet(string sql, params SqlParameter[] ps)
{
DataSet ds = new DataSet();
using (SqlDataAdapter sda = new SqlDataAdapter(sql, connStr))
{
sda.SelectCommand.Parameters.AddRange(ps);
sda.Fill(ds);
}
return ds;
}
}
本文来自博客园,作者:NE_STOP,转载请注明原文链接:https://www.cnblogs.com/alineverstop/p/18004730