예제 #1
0
        private static ConnectionStringElement GetConfiguration(string name)
        {
            ConnectionManagerConfigurationSection section =
                ConnectionManagerConfigurationSection.GetConfig();

            ConnectionStringConfigurationElement configElement = section.ConnectionStrings[name];
            ConnectionStringElement element = null;

            // 本地配置文件的连接串采用优先级高于Meta库中的
            if (configElement != null)
            {
                element = new ConnectionStringElement();
                element.ConnectionString = configElement.ConnectionString;
                element.ProviderName     = configElement.ProviderName;
                element.EventArgsType    = configElement.EventArgs.Type;
                element.Name             = configElement.Name;
                element.CommandTimeout   = configElement.CommandTimeout;
            }
            //else
            //{
            //    // 备选通道是从Meta库获取
            //    MetaConnectionStringConfigurationElement metaElement = section.MetaConnectionString;
            //    if (metaElement != null)
            //        element = metaElement.GetConnectionStringElement(name);
            //}

            return(element);
        }
예제 #2
0
        /// <summary>
        /// 数据库联接是否定义
        /// </summary>
        /// <param name="name"></param>
        public static bool ConnectionNameIsConfiged(string name)
        {
            ExceptionHelper.CheckStringIsNullOrEmpty(name, "name");

            ConnectionStringElement settings = GetConfiguration(name);

            return(settings != null);
        }
예제 #3
0
        /// <summary>
        /// 获取指定连接的DbProviderFactory对象(Added by Shen Zheng)
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        internal static DbProviderFactory GetDbProviderFactory(string name)
        {
            ExceptionHelper.CheckStringIsNullOrEmpty(name, "name");

            ConnectionStringElement elem = GetConfiguration(name);

            if (elem == null)
            {
                throw new ConfigurationErrorsException(string.Format(Resource.CanNotFindConnectionName, name));
            }

            return(DbProviderFactories.GetFactory(elem.ProviderName));
        }
예제 #4
0
        /// <summary>
        /// 根据数据库逻辑名称获得连接实例
        /// </summary>
        /// <param name="name">数据库逻辑名称</param>
        /// <returns>数据库连接实例</returns>
        internal static DbConnection GetConnection(string name)
        {
            ExceptionHelper.CheckStringIsNullOrEmpty(name, "name");

            ConnectionStringElement settings = GetConfiguration(name);

            if (settings == null)
            {
                throw new ConfigurationErrorsException(string.Format(Resource.CanNotFindConnectionName, name));
            }

            DbConnection dbConnection = DbProviderFactories.GetFactory(settings.ProviderName).CreateConnection();

            dbConnection.ConnectionString = settings.ConnectionString;

            return(dbConnection);
        }