个人开发:
<appSettings>
<add key="conindex" value="1"/>
<add key="sqlconstr" value="server=.\SQLEXPRESS;database=MyOffice;uid=sa;pwd=zqmingok"/>
<add key="OLEDBCONNECTIONSTRING" value="provider=microsoft.jet.oledb.4.0;data source="/>
<add key="dbPath" value="~/App_Data/Data.mdb"/>
</appSettings>
Code
using System;
using System.Data;
using System.Data.OleDb;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public class SQLHelper
{
private static string keystr = ConfigurationManager.AppSettings[0].ToString();
private int conindex = Convert.ToInt32(keystr);
private SqlConnection sqlcon = null;
private SqlCommand sqlcom = null;
private SqlDataReader sdr = null;
/// <summary>
/// 初始化方法
/// </summary>
public SQLHelper()
{
//string constr=ConfigurationManager.ConnectionStrings[1].ConnectionString;
sqlcon = new SqlConnection(getConStr(1));
}
/// <summary>
/// 获取数据库连接字段
/// </summary>
/// <param name="conindex">web.config中的配制字符串按顺序分别为0,1,2,3.</param>
/// <returns>返回数据库连接字符串</returns>
private string getConStr(int Tindex)
{
if (Tindex == 1 || Tindex == 2)
{
if (Tindex == 1)
{
string sqlconstr = ConfigurationManager.AppSettings[1].ToString();
return sqlconstr;
}
else
{
string oledconstr = ConfigurationManager.AppSettings[2].ToString();
string oleddbstr = System.Web.HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings[3].ToString());
return oledconstr + oleddbstr;
}
}
else
{
return null;
}
}
/// <summary>
/// 执行增删改操作
/// </summary>
/// <param name="sql">传入的SQL</param>
/// <returns>返回影响的行数</returns>
public int ExecuteNonQuery(string sql)
{
int res;
try
{
sqlcom = new SqlCommand(sql, getCon());
res = sqlcom.ExecuteNonQuery();
}
catch (Exception ex)
{
throw ex;
}
finally
{
closecon();
}
return res;
}
public int ExecuteNonQuery(string sql, SqlParameter[] paras)
{
int res;
try
{
sqlcom = new SqlCommand(sql, getCon());
sqlcom.Parameters.AddRange(paras);
res = sqlcom.ExecuteNonQuery();
}
catch (Exception ex)
{
throw ex;
}
finally
{
closecon();
}
return res;
}
/// <summary>
/// 执行查询
/// </summary>
/// <param name="sql">传入SQL</param>
/// <returns>返回TABLE</returns>
public DataTable ExecuteQuery(string sql)
{
DataTable dt = new DataTable();
sqlcom = new SqlCommand(sql, getCon());
using (sdr = sqlcom.ExecuteReader(CommandBehavior.CloseConnection))
{ dt.Load(sdr); }
closecon();
return dt;
}
public DataTable ExecuteQuery(string sql, SqlParameter[] paras)
{
DataTable dt = new DataTable();
sqlcom = new SqlCommand(sql, getCon());
sqlcom.Parameters.AddRange(paras);
using (sdr = sqlcom.ExecuteReader(CommandBehavior.CloseConnection))
{ dt.Load(sdr); }
closecon();
return dt;
}
/// <summary>
/// datereader
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public SqlDataReader ExecuteDataReaderQuery(string sql)
{
sqlcom = new SqlCommand(sql, getCon());
using (sdr = sqlcom.ExecuteReader(CommandBehavior.CloseConnection))
{ return sdr; }
closecon();
}
public SqlDataReader ExecuteDataReaderQuery(string sql, SqlParameter[] paras)
{
sqlcom = new SqlCommand(sql, getCon());
sqlcom.Parameters.AddRange(paras);
using (sdr = sqlcom.ExecuteReader(CommandBehavior.CloseConnection))
{ return sdr; }
closecon();
}
/// <summary>
/// 执行PROCEDURE
/// </summary>
/// <param name="proc"></param>
/// <returns></returns>
public DataTable ExecuteQueryProc(string proc)
{
DataTable dt = new DataTable();
sqlcom = new SqlCommand(proc, getCon());
sqlcom.CommandType = CommandType.StoredProcedure;
// sqlcom.Parameters.AddRange(paras);
using (sdr = sqlcom.ExecuteReader(CommandBehavior.CloseConnection))
{ dt.Load(sdr); }
closecon();
return dt;
}
public DataTable ExecuteQueryProc(string proc, SqlParameter[] paras)
{
DataTable dt = new DataTable();
sqlcom = new SqlCommand(proc, getCon());
sqlcom.CommandType = CommandType.StoredProcedure;
sqlcom.Parameters.AddRange(paras);
using (sdr = sqlcom.ExecuteReader(CommandBehavior.CloseConnection))
{ dt.Load(sdr); }
closecon();
return dt;
}
public int ExecuteNonQueryProc(string proc)
{
int res;
try
{
sqlcom = new SqlCommand(proc, getCon());
sqlcom.CommandType = CommandType.StoredProcedure;
res = sqlcom.ExecuteNonQuery();
}
catch (Exception ex)
{
throw ex;
}
finally
{
closecon();
}
return res;
}
public int ExecuteNonQueryProc(string proc, SqlParameter[] paras)
{
int res;
try
{
sqlcom = new SqlCommand(proc, getCon());
sqlcom.CommandType = CommandType.StoredProcedure;
sqlcom.Parameters.AddRange(paras);
res = sqlcom.ExecuteNonQuery();
}
catch (Exception ex)
{
throw ex;
}
finally
{
closecon();
}
return res;
}
/// <summary>
/// 得到相关CONNECTION
/// </summary>
/// <returns></returns>
private SqlConnection getCon()
{
if (sqlcon.State == ConnectionState.Closed)
{
sqlcon.Open();
}
return sqlcon;
}
/// <summary>
/// 关闭连接的对象
/// </summary>
private void closecon()
{
if (sqlcon.State == ConnectionState.Open)
{
sqlcon.Close();
}
}
}
using System;
using System.Data;
using System.Data.OleDb;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public class SQLHelper
{
private static string keystr = ConfigurationManager.AppSettings[0].ToString();
private int conindex = Convert.ToInt32(keystr);
private SqlConnection sqlcon = null;
private SqlCommand sqlcom = null;
private SqlDataReader sdr = null;
/// <summary>
/// 初始化方法
/// </summary>
public SQLHelper()
{
//string constr=ConfigurationManager.ConnectionStrings[1].ConnectionString;
sqlcon = new SqlConnection(getConStr(1));
}
/// <summary>
/// 获取数据库连接字段
/// </summary>
/// <param name="conindex">web.config中的配制字符串按顺序分别为0,1,2,3.</param>
/// <returns>返回数据库连接字符串</returns>
private string getConStr(int Tindex)
{
if (Tindex == 1 || Tindex == 2)
{
if (Tindex == 1)
{
string sqlconstr = ConfigurationManager.AppSettings[1].ToString();
return sqlconstr;
}
else
{
string oledconstr = ConfigurationManager.AppSettings[2].ToString();
string oleddbstr = System.Web.HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings[3].ToString());
return oledconstr + oleddbstr;
}
}
else
{
return null;
}
}
/// <summary>
/// 执行增删改操作
/// </summary>
/// <param name="sql">传入的SQL</param>
/// <returns>返回影响的行数</returns>
public int ExecuteNonQuery(string sql)
{
int res;
try
{
sqlcom = new SqlCommand(sql, getCon());
res = sqlcom.ExecuteNonQuery();
}
catch (Exception ex)
{
throw ex;
}
finally
{
closecon();
}
return res;
}
public int ExecuteNonQuery(string sql, SqlParameter[] paras)
{
int res;
try
{
sqlcom = new SqlCommand(sql, getCon());
sqlcom.Parameters.AddRange(paras);
res = sqlcom.ExecuteNonQuery();
}
catch (Exception ex)
{
throw ex;
}
finally
{
closecon();
}
return res;
}
/// <summary>
/// 执行查询
/// </summary>
/// <param name="sql">传入SQL</param>
/// <returns>返回TABLE</returns>
public DataTable ExecuteQuery(string sql)
{
DataTable dt = new DataTable();
sqlcom = new SqlCommand(sql, getCon());
using (sdr = sqlcom.ExecuteReader(CommandBehavior.CloseConnection))
{ dt.Load(sdr); }
closecon();
return dt;
}
public DataTable ExecuteQuery(string sql, SqlParameter[] paras)
{
DataTable dt = new DataTable();
sqlcom = new SqlCommand(sql, getCon());
sqlcom.Parameters.AddRange(paras);
using (sdr = sqlcom.ExecuteReader(CommandBehavior.CloseConnection))
{ dt.Load(sdr); }
closecon();
return dt;
}
/// <summary>
/// datereader
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public SqlDataReader ExecuteDataReaderQuery(string sql)
{
sqlcom = new SqlCommand(sql, getCon());
using (sdr = sqlcom.ExecuteReader(CommandBehavior.CloseConnection))
{ return sdr; }
closecon();
}
public SqlDataReader ExecuteDataReaderQuery(string sql, SqlParameter[] paras)
{
sqlcom = new SqlCommand(sql, getCon());
sqlcom.Parameters.AddRange(paras);
using (sdr = sqlcom.ExecuteReader(CommandBehavior.CloseConnection))
{ return sdr; }
closecon();
}
/// <summary>
/// 执行PROCEDURE
/// </summary>
/// <param name="proc"></param>
/// <returns></returns>
public DataTable ExecuteQueryProc(string proc)
{
DataTable dt = new DataTable();
sqlcom = new SqlCommand(proc, getCon());
sqlcom.CommandType = CommandType.StoredProcedure;
// sqlcom.Parameters.AddRange(paras);
using (sdr = sqlcom.ExecuteReader(CommandBehavior.CloseConnection))
{ dt.Load(sdr); }
closecon();
return dt;
}
public DataTable ExecuteQueryProc(string proc, SqlParameter[] paras)
{
DataTable dt = new DataTable();
sqlcom = new SqlCommand(proc, getCon());
sqlcom.CommandType = CommandType.StoredProcedure;
sqlcom.Parameters.AddRange(paras);
using (sdr = sqlcom.ExecuteReader(CommandBehavior.CloseConnection))
{ dt.Load(sdr); }
closecon();
return dt;
}
public int ExecuteNonQueryProc(string proc)
{
int res;
try
{
sqlcom = new SqlCommand(proc, getCon());
sqlcom.CommandType = CommandType.StoredProcedure;
res = sqlcom.ExecuteNonQuery();
}
catch (Exception ex)
{
throw ex;
}
finally
{
closecon();
}
return res;
}
public int ExecuteNonQueryProc(string proc, SqlParameter[] paras)
{
int res;
try
{
sqlcom = new SqlCommand(proc, getCon());
sqlcom.CommandType = CommandType.StoredProcedure;
sqlcom.Parameters.AddRange(paras);
res = sqlcom.ExecuteNonQuery();
}
catch (Exception ex)
{
throw ex;
}
finally
{
closecon();
}
return res;
}
/// <summary>
/// 得到相关CONNECTION
/// </summary>
/// <returns></returns>
private SqlConnection getCon()
{
if (sqlcon.State == ConnectionState.Closed)
{
sqlcon.Open();
}
return sqlcon;
}
/// <summary>
/// 关闭连接的对象
/// </summary>
private void closecon()
{
if (sqlcon.State == ConnectionState.Open)
{
sqlcon.Close();
}
}
}