public static string connectionString = ConfigurationManager.ConnectionStrings["SQLConnString"].ToString();
public static int ExecuteNonQuery(string sql, SqlParameter[] pars)
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
if (conn.State != ConnectionState.Open) conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.CommandTimeout = 180;
if (pars != null)
{
foreach (SqlParameter p in pars)
{
cmd.Parameters.Add(p);
}
}
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
}
}
public static SqlDataReader SqlDataReader(string sql, SqlParameter[] pars)
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
if (conn.State != ConnectionState.Open) conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.CommandTimeout = 180;
if (pars != null)
{
foreach (SqlParameter p in pars)
{
cmd.Parameters.Add(p);
}
}
SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
cmd.Parameters.Clear();
return dr;
}
}
public static object ExecuteScalar(string sql, SqlParameter[] pars)
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.CommandTimeout = 180;
if (pars != null)
{
foreach (SqlParameter p in pars)
{
cmd.Parameters.Add(p);
}
}
object obj = cmd.ExecuteScalar();
cmd.Parameters.Clear();
return obj;
}
}
public static DataTable GetTable(string sql, SqlParameter[] pars)
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
if (conn.State != ConnectionState.Open) conn.Open();
SqlDataAdapter adap = new SqlDataAdapter(sql, conn);
if (pars != null)
{
foreach (SqlParameter p in pars)
{
adap.SelectCommand.Parameters.Add(p);
}
}
DataTable dt = new DataTable();
adap.Fill(dt);
return dt;
}
}