예제 #1
0
        /// <summary>
        /// 使用若干参数初始化数据连接参数。
        /// </summary>
        /// <param name="Parameters">参数,包括DbProviderFactory(String类型表示的完整限定名称或DbProviderFactory类型)、DbAdapter(String类型表示的类型完整限定名称或DbAdapter类型),及连接字符串(ConnectionString)或当前IDbAdapter.BuildConnectionString所需参数。</param>
        /// <returns>初始化成功时,返回DbProvider,否则,返回null。</returns>
        public static DbProvider Initialize(NameValueList Parameters)
        {
            DbConnection _conn = null;
            DbProvider   res   = new DbProvider();

            try
            {
                IDbAdapter DbAdapter = _getInstance <IDbAdapter>(Parameters["DbAdapter"]);
                if (DbAdapter != null)
                {
                    res._config["DbAdapter"] = DbAdapter;
                    DbAdapter.InitializeOptions(res.Options);
                }
                DbProviderFactory DbProviderFactory = _getInstance <DbProviderFactory>(Parameters["DbProviderFactory"]);
                if (DbProviderFactory != null)
                {
                    res._config["DbProviderFactory"] = DbProviderFactory;
                }
                else if (res.Options["DefaultDbProviderFactory"] != null)
                {
                    res._config["DbProviderFactory"] = _getInstance <DbProviderFactory>(res.Options["DefaultDbProviderFactory"]);
                }
                if (Parameters["ConnectionString"] is string)
                {
                    res._config["ConnectionString"] = Parameters["ConnectionString"];
                }
                else
                {
                    res._config["ConnectionString"] = res.Adapter.BuildConnectionString(Parameters);
                }
                _conn = res.Provider.CreateConnection();
                _conn.ConnectionString = res.ConnectionString;
                _conn.Open();
                res._config["DbSchema"] = res.Adapter.GetSchema(_conn);
                if (Current == null)
                {
                    Current = res;
                }
                return(res);
            }
            catch (Exception e)
            {
                #if DEBUG
                DbConnectException dce = new DbConnectException(e, res._config);
                Debug.WriteLine(dce);
                throw dce;
                #endif

                return(null);
            }
            finally
            {
                try
                {
                    _conn.Close();
                }
                catch { }
            }
        }
예제 #2
0
        /// <summary>
        /// 使用连接字符串、DbProviderFactory、DbAdapter初始化数据连接参数。
        /// </summary>
        /// <param name="ConnectionString">连接字符串</param>
        /// <param name="DbProviderFactory">DbProviderFactory</param>
        /// <param name="DbAdapter">DbAdapter</param>
        /// <returns>初始化成功时,返回DbProvider,否则,返回null。</returns>
        public static DbProvider Initialize(string ConnectionString, DbProviderFactory DbProviderFactory, IDbAdapter DbAdapter)
        {
            DbConnection _conn = null;
            DbProvider   res   = new DbProvider();

            try
            {
                if (DbAdapter != null)
                {
                    res._config["DbAdapter"] = DbAdapter;
                    DbAdapter.InitializeOptions(res.Options);
                }
                if (DbProviderFactory != null)
                {
                    res._config["DbProviderFactory"] = DbProviderFactory;
                }
                else if (res.Options["DefaultDbProviderFactory"] != null)
                {
                    res._config["DbProviderFactory"] = _getInstance <DbProviderFactory>(res.Options["DefaultDbProviderFactory"]);
                }
                if (!string.IsNullOrEmpty(ConnectionString))
                {
                    res._config["ConnectionString"] = ConnectionString;
                }
                _conn = res.Provider.CreateConnection();
                _conn.ConnectionString = res.ConnectionString;
                _conn.Open();
                res._config["DbSchema"] = res.Adapter.GetSchema(_conn);
                if (Current == null)
                {
                    Current = res;
                }
                return(res);
            }
            catch (Exception e)
            {
                #if DEBUG
                DbConnectException dce = new DbConnectException(e, res._config);
                Debug.WriteLine(dce);
                throw dce;
                #endif

                return(null);
            }
            finally
            {
                try
                {
                    _conn.Close();
                }
                catch { }
            }
        }