using
System;
using
System.IO;
using
System.Data;
using
System.Data.SqlClient;
using
System.Web;
using
System.Web.UI;
using
System.Collections;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace
test
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// DbAccess 的摘要说名。
/// </summary>
public class DbAccess
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private bool disposed = false;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public DbAccess()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//
// TODO: 在此处添加构造函数逻辑
//
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private static string dbconnectstring = System.Configuration.ConfigurationSettings.AppSettings.Get("strConn");
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private static void curPage_Unload(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Release();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public static Hashtable innerHash=new Hashtable();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public static DbAccess Instance
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Page curPage=(Page)HttpContext.Current.Handler;
if(innerHash.Contains(curPage))
return (DbAccess)innerHash[curPage];
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
DbAccess obj=new DbAccess();
innerHash[curPage]=obj;
curPage.Unload+=new EventHandler(curPage_Unload);
return obj;
}
}
}
private static void Release()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Page curPage=(Page)HttpContext.Current.Handler;
if(innerHash.Contains(curPage))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
((DbAccess)innerHash[curPage]).Dispose();
innerHash.Remove(curPage);
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public static DbAccess GetObject(object obj)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(innerHash.Contains(obj))
return (DbAccess)innerHash[obj];
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
DbAccess dbobj=new DbAccess();
innerHash[obj]=dbobj;
return dbobj;
}
}
public static void Release(object obj)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(innerHash.Contains(obj))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
((DbAccess)innerHash[obj]).Dispose();
innerHash.Remove(obj);
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private SqlConnection innerCn=null;
public System.Data.SqlClient.SqlConnection GetConn()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(innerCn==null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
innerCn=new SqlConnection(dbconnectstring);
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
innerCn.Open();
}
catch(Exception)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
// MessageBox("打开数据库连接失败!");
return null;
}
}
return innerCn;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
///执行SQL语句,返回DataTable对象。
/// </summary>
/// <param name="strSQL">要执行的SQL语句,为字符串类型string</param>
/// <returns>返回DataTable对象,由函数调用者主动dispose</returns>
public System.Data.DataTable ExecuteSql(string strSQL)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
SqlConnection myCn =null;
System.Data.SqlClient.SqlDataAdapter objDa=null;
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
myCn=GetConn();
if(myCn.State==ConnectionState.Closed) myCn.Open();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
objDa=new SqlDataAdapter(strSQL,myCn);
DataSet objDs=new DataSet();
objDa.Fill(objDs,"0");
return objDs.Tables["0"];
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
catch(Exception e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new Exception(e.Message);
}
finally
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
objDa.Dispose();
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 判断是否有指定名称的表,判断是否有指定的字段
/// </summary>
/// <param name="strSQL">(string)</param>
/// <returns>(int)</returns>
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
/// <summary>
/// 按SQL语句返回DataReader对象
/// </summary>
/// <param name="strSQL">(string)</param>
/// <returns>(System.Data.SqlClient.SqlDataReader)</returns>
public SqlDataReader ExecuteSqlReader(string strSql)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
SqlConnection myCn=GetConn();
SqlCommand myCmd=new SqlCommand(strSql,myCn);
if(myCn.State==ConnectionState.Closed) myCn.Open();
SqlDataReader objReader=myCmd.ExecuteReader();
return objReader;
}
catch(Exception)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return null;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public SqlDataReader ExecuteSqlReaderMember(string strSql)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
SqlConnection myCn=GetConn();
SqlCommand myCmd=new SqlCommand(strSql,myCn);
if(myCn.State==ConnectionState.Closed) myCn.Open();
SqlDataReader objReader=myCmd.ExecuteReader();
return objReader;
}
catch(Exception)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return null;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 执行SQL语句,返回数据集的第一行第一列的值(object)。
/// </summary>
/// <param name="strSQL">(string)</param>
/// <returns>(object)</returns>
public object ExecuteSqlEx1(string strSQL)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
SqlConnection myCn = GetConn();
SqlCommand myCmd = new SqlCommand(strSQL,myCn);
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(myCn.State==ConnectionState.Closed) myCn.Open();
object r = myCmd.ExecuteScalar();
if(Object.Equals(r,null))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new Exception("无有效值");
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return r;
}
}
catch(System.Data.SqlClient.SqlException)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return null;
}
finally
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
myCmd.Dispose();
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 执行SQL语句,返回数据集的第一行第一列的值(int)。
/// </summary>
/// <param name="strSQL">(string)</param>
/// <returns>(int)</returns>
public int ExecuteSqlEx2(string strSQL)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
SqlConnection myCn = GetConn();
SqlCommand myCmd = new SqlCommand(strSQL,myCn);
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(myCn.State==ConnectionState.Closed) myCn.Open();
object r = myCmd.ExecuteScalar();
if(Object.Equals(r,null))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new Exception("无有效值");
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return Convert.ToInt32(r);
}
}
catch(System.Data.SqlClient.SqlException e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new Exception(e.Message);
}
finally
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
myCmd.Dispose();
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 执行SQL语句,不返回任何结果。
/// </summary>
/// <param name="strSQL">(string)</param>
/// <returns>bool</returns>
public bool ExecuteSqlNoneResult(string strSQL)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
SqlConnection myCn = GetConn();
SqlCommand myCmd = new SqlCommand(strSQL,myCn);
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(myCn.State==ConnectionState.Closed) myCn.Open();
myCmd.ExecuteNonQuery();
}
catch(System.Data.SqlClient.SqlException e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new Exception(e.Message);
}
finally
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
myCmd.Dispose();
}
return true;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
IDisposable 成员#region IDisposable 成员
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public void Dispose()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Dispose(true);
GC.SuppressFinalize(this);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private void Dispose(bool disposing)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(!this.disposed)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(disposing)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(innerCn!=null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(innerCn.State==ConnectionState.Open)
innerCn.Close();
innerCn.Dispose();
}
}
}
disposed = true;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
}
转载于:https://www.cnblogs.com/zhixiaoning/archive/2008/02/10/1066518.html