我的DBProxy

    技术2026-01-18  4

    1.为什么要使用DBProxy?

       SQLHelper在使用时要指定ConnectionString,如果每次在使用时都指定,将很麻烦;

       大部分情况下,一个程序中只连接一个数据库,ConnectionString是同一个,因此可以建立一个SQLHelper代理类,在这里指定数据库连接;

       程序中只需要有一个DBProxy对象,为提高性能,用Singleton实现;

    2.代码

    using System; using System.Collections.Generic; using System.Text; using System.Data.SqlClient; using System.Data; using DBAccess; namespace BL { public class DBProxy { private string _strConstring; private SQLHelper _sqlHelper; private static readonly string g_ConnectionString1 = System.Web.Configuration.WebConfigurationManager.ConnectionStrings[0].ConnectionString; private static readonly DBProxy g_Instance1 = new DBProxy(); private DBProxy() { _strConstring = g_ConnectionString1; } public static DBProxy GetInstance1() { return g_Instance1; } public static SQLHelper GetSQLHelper() { return new SQLHelper(g_ConnectionString1); } public static SqlParameter CreateParam(string strName, SqlDbType type, int intSize, ParameterDirection direction, object objValue) { return SQLHelper.CreateParam(strName, type, intSize, direction, objValue); } public bool ExecNonQuery(bool blnIsProcedure, string strSqlCommand, params SqlParameter[] aryParams) { try { using (_sqlHelper = new SQLHelper(_strConstring)) { _sqlHelper.ExecNonQuery(blnIsProcedure, strSqlCommand, aryParams); } } catch { return false; } return true; } public DataSet QueryDataSet(bool blnIsProcedure, string strSqlCommand, params SqlParameter[] aryParams) { using (_sqlHelper = new SQLHelper(_strConstring)) { return _sqlHelper.QueryDataSet(blnIsProcedure, strSqlCommand, aryParams); } } public object QueryValue(bool blnIsProcedure, string strSqlCommand, params SqlParameter[] aryParams) { using (_sqlHelper = new SQLHelper(_strConstring)) { return _sqlHelper.QueryValue(blnIsProcedure, strSqlCommand, aryParams); } } } }

    最新回复(0)