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); }
/// <summary> /// 数据库联接是否定义 /// </summary> /// <param name="name"></param> public static bool ConnectionNameIsConfiged(string name) { ExceptionHelper.CheckStringIsNullOrEmpty(name, "name"); ConnectionStringElement settings = GetConfiguration(name); return(settings != null); }
protected virtual void Parse() { this.settings = new ConnectionStringElement(); ParseOriginalSettings(); ParseBuilders(); ParseEventArgs(); isParsed = true; }
/// <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); ExceptionHelper.FalseThrow <ConfigurationException>(elem != null, Resource.CanNotFindConnectionName, name); return(DbProviderFactories.GetFactory(elem.ProviderName)); }
/// <summary> /// 根据数据库逻辑名称获得连接实例 /// </summary> /// <param name="name">数据库逻辑名称</param> /// <returns>数据库连接实例</returns> internal static DbConnection GetConnection(string name) { ExceptionHelper.CheckStringIsNullOrEmpty(name, "name"); ConnectionStringElement settings = GetConfiguration(name); ExceptionHelper.FalseThrow <ConfigurationException>(settings != null, Resource.CanNotFindConnectionName, name); DbConnection dbConnection = DbProviderFactories.GetFactory(settings.ProviderName).CreateConnection(); dbConnection.ConnectionString = settings.ConnectionString; return(dbConnection); }
protected override void ParseBuilders() { if (isMetaConnectionParsed) { return; } isMetaConnectionParsed = true; // 配置文件的解析好像成问题,保险的办法就是在这里try一下重新解析 if (settings == null) { settings = new ConnectionStringElement(); settings.Name = (string)this["name"]; settings.ConnectionString = (string)this["connectionString"]; settings.ProviderName = (string)this["providerName"]; } ParseSettingBuildersByPrefix(MetaSectionPrefix, ref settings); TranslateMetaConnections(); }
private void TranslateMetaConnections() { DataTable result = LoadMetaConnections(); if (result == null) { return; } metaConnectionSettings = new Dictionary <string, ConnectionStringElement>(); foreach (DataRow row in result.Rows) { ConnectionStringElement setting = new ConnectionStringElement(); setting.Name = row["NAME"] as string; setting.ConnectionString = row["CONNECTION_STRING"] as string; setting.ProviderName = row["PROVIDER_NAME"] as string; setting.EventArgsType = row["EVENT_ARGUMENT_TYPE"] as string; ParseSettingBuildersByPrefix(ItemSectionPrefix, ref setting); metaConnectionSettings.Add(setting.Name, setting); } }
private void ParseSettingBuildersByPrefix(string prefix, ref ConnectionStringElement setting) { if ((Builders != null) && (Builders.Count > 0)) { foreach (BuilderConfigurationElement elementBuilder in Builders) { if ((elementBuilder.Name.StartsWith(prefix)) || (elementBuilder.Name.StartsWith(BothSectionPrefix))) { ConnectionStringBuilderBase builder = (ConnectionStringBuilderBase)TypeCreator.CreateInstance(elementBuilder.Type); switch (elementBuilder.AttributeName) { case "connectionString": setting.ConnectionString = builder.BuildUp(setting.ConnectionString); break; case "name": setting.Name = builder.BuildUp(setting.Name); break; case "providerName": setting.ProviderName = builder.BuildUp(setting.ProviderName); break; } } } } }