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;
}

}
}


编辑器加载中...

转载于:https://www.cnblogs.com/xiaohaoblog/archive/2012/04/05/2433894.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值