ASP.Net DBHelper
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace DAL
{
public class DBHelper
{
private static SqlConnection con;
private static string conString = ConfigurationManager.ConnectionStrings["con"].ConnectionString;
private static SqlConnection Con
{
get
{
if (con == null)
{
con = new SqlConnection(conString);
con.Open();
}
else if (con.State == ConnectionState.Broken)
{
con.Close();
con.Open();
}
else if (con.State == ConnectionState.Closed)
{
con.Open();
}
return con;
}
}
private static SqlCommand Com
{
get {
return Con.CreateCommand();
}
}
public static bool Update(string sql)
{
SqlCommand cmd = Com;
cmd.CommandText = sql;
return cmd.ExecuteNonQuery() > 0;
}
public static bool Update(string sql,SqlParameter[] pars)
{
SqlCommand cmd = Com;
cmd.CommandText = sql;
cmd.Parameters.AddRange(pars);
return cmd.ExecuteNonQuery() > 0;
}
public static bool Update(string sql, SqlParameter[] pars,bool isNewCon,SqlConnection _con)
{
if (isNewCon)
{
_con = new SqlConnection(conString);
_con.Open();
SqlCommand cmd = _con.CreateCommand();
cmd.CommandText = sql;
cmd.Parameters.AddRange(pars);
return cmd.ExecuteNonQuery() > 0;
}
else
{
SqlCommand cmd = Com;
cmd.CommandText = sql;
cmd.Parameters.AddRange(pars);
return cmd.ExecuteNonQuery() > 0;
}
}
public static bool Update(string sql,bool isNewCon, SqlConnection _con)
{
if (isNewCon)
{
_con = new SqlConnection(conString);
_con.Open();
SqlCommand cmd = _con.CreateCommand();
cmd.CommandText = sql;
return cmd.ExecuteNonQuery() > 0;
}
else
{
SqlCommand cmd = Com;
cmd.CommandText = sql;
return cmd.ExecuteNonQuery() > 0;
}
}
public static object SelectForScalar(string sql)
{
SqlCommand cmd = Com;
cmd.CommandText = sql;
return cmd.ExecuteScalar();
}
public static object SelectForScalar(string sql,SqlParameter[] pars)
{
SqlCommand cmd = Com;
cmd.CommandText = sql;
cmd.Parameters.AddRange(pars);
return cmd.ExecuteScalar();
}
public static object SelectForScalar(string sql, SqlParameter[] pars,bool isNewCon,SqlConnection _con)
{
if (isNewCon)
{
_con = new SqlConnection(conString);
_con.Open();
SqlCommand cmd = _con.CreateCommand();
cmd.CommandText = sql;
cmd.Parameters.AddRange(pars);
return cmd.ExecuteScalar();
}
else
{
SqlCommand cmd = Com;
cmd.CommandText = sql;
cmd.Parameters.AddRange(pars);
return cmd.ExecuteScalar();
}
}
public static object SelectForScalar(string sql, bool isNewCon, SqlConnection _con)
{
if (isNewCon)
{
_con = new SqlConnection(conString);
_con.Open();
SqlCommand cmd = _con.CreateCommand();
cmd.CommandText = sql;
return cmd.ExecuteScalar();
}
else
{
SqlCommand cmd = Com;
cmd.CommandText = sql;
return cmd.ExecuteScalar();
}
}
public static SqlDataReader SelectForDataReader(string sql)
{
SqlCommand cmd = Com;
cmd.CommandText = sql;
return cmd.ExecuteReader();
}
public static SqlDataReader SelectForDataReader(string sql,SqlParameter[] pars)
{
SqlCommand cmd = Com;
cmd.CommandText = sql;
cmd.Parameters.AddRange(pars);
return cmd.ExecuteReader();
}
public static SqlDataReader SelectForDataReader(string sql, SqlParameter[] pars, bool isNewCon, SqlConnection _con)
{
if (isNewCon)
{
_con = new SqlConnection(conString);
_con.Open();
SqlCommand cmd = _con.CreateCommand();
cmd.CommandText = sql;
cmd.Parameters.AddRange(pars);
return cmd.ExecuteReader();
}
else
{
SqlCommand cmd = Com;
cmd.CommandText = sql;
cmd.Parameters.AddRange(pars);
return cmd.ExecuteReader();
}
}
public static SqlDataReader SelectForDataReader(string sql, bool isNewCon, SqlConnection _con)
{
if (isNewCon)
{
_con = new SqlConnection(conString);
_con.Open();
SqlCommand cmd = _con.CreateCommand();
cmd.CommandText = sql;
return cmd.ExecuteReader();
}
else
{
SqlCommand cmd = Com;
cmd.CommandText = sql;
return cmd.ExecuteReader();
}
}
public static bool UpdateProc(string sql, SqlParameter[] pars)
{
SqlCommand cmd = Com;
cmd.CommandText = sql;
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddRange(pars);
return cmd.ExecuteNonQuery() > 0;
}
}
}
编辑器加载中...