Exemplo n.º 1
0
 /// <summary>
 /// 数据库操作类
 /// </summary>
 /// <param name="connectionString">数据库连接字符串</param>
 public DBHelper(string connectionString)
 {
     if (!string.IsNullOrWhiteSpace(connectionString))
     {
         _connectionString = connectionString;
     }
     else
     {
         _connectionString = DbConnections.GetDbConnectionString();
     }
 }
Exemplo n.º 2
0
        /// <summary>
        /// 创建DBHelper实例
        /// </summary>
        /// <param name="connectionString">指定连接字符串</param>
        /// <param name="dataBaseType">指定数据库类型</param>
        /// <returns></returns>
        public static IDBHelper GetDBHelper(string connectionString = null, DataBaseType dataBaseType = DataBaseType.Auto)
        {
            IDBHelper helper = null;

            if (dataBaseType == DataBaseType.Auto)
            {
                dataBaseType = DbConnections.DbType;
            }

            Monitor.Enter(_dbHelperCache);
            if (_dbHelperCache.ContainsKey(dataBaseType))
            {
                Monitor.Exit(_dbHelperCache);
                return(_dbHelperCache[dataBaseType]);
            }

            if (connectionString == null)
            {
                connectionString = DbConnections.GetDbConnectionString(dataBaseType);
            }

            switch (dataBaseType)
            {
            case DataBaseType.SqlServer:
                helper = ReflectHelper.GetInstance("SQLHelper.dll", "SQLHelper.SQLHelper", connectionString) as IDBHelper;
                break;

            case DataBaseType.MySql:
                helper = ReflectHelper.GetInstance("MySQLHelper.dll", "MySQLHelper.MySQLHelper", connectionString) as IDBHelper;
                break;

            case DataBaseType.Oracle:
                helper = ReflectHelper.GetInstance("OracleHelper.dll", "OracleHelper.OracleHelper", connectionString) as IDBHelper;
                break;

            case DataBaseType.Sqlite:
                helper = ReflectHelper.GetInstance("SQLiteHelper.dll", "SQLiteHelper.SQLiteHelper", connectionString) as IDBHelper;
                break;

            default:
                Monitor.Exit(_dbHelperCache);
                throw new Exception("未知数据库类型");
            }
            if (helper != null)
            {
                _dbHelperCache.Add(dataBaseType, helper);
            }
            Monitor.Exit(_dbHelperCache);
            return(helper);
        }