/// <summary> /// 根据指定的provider 和 connectionString 创建对应的链接数据库,不从本地配置文件中创建. /// </summary> /// <param name="provider"></param> /// <param name="connectionString"></param> /// <returns></returns> public static Database CreateDatabase(string provider, string connectionString) { if (string.IsNullOrEmpty(provider)) { throw new MB.Util.APPException("创建Database 时 provider 不能为空,请检查", Util.APPMessageType.SysErrInfo); } if (string.IsNullOrEmpty(connectionString)) { throw new MB.Util.APPException("创建Database 时 connectionString 不能为空,请检查", Util.APPMessageType.SysErrInfo); } try { var databaseSettings = new Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings(); var connStringSection = new System.Configuration.ConnectionStringsSection(); var dictDataSource = new Microsoft.Practices.EnterpriseLibrary.Common.Configuration.DictionaryConfigurationSource(); //针对SqLite provider 的特殊配置 var dbProvider = new Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DbProviderMapping("System.Data.SQLite", "EntLibContrib.Data.SQLite.SQLiteDatabase, EntLibContrib.Data.SqLite"); connStringSection.ConnectionStrings.Add(new System.Configuration.ConnectionStringSettings(DEFAULT_DB_NAME, connectionString, provider)); databaseSettings.ProviderMappings.Add(dbProvider); databaseSettings.DefaultDatabase = DEFAULT_DB_NAME; dictDataSource.Add(Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings.SectionName, databaseSettings); dictDataSource.Add("connectionStrings", connStringSection); var dbFactory = new DatabaseProviderFactory(dictDataSource); var database = dbFactory.Create(DEFAULT_DB_NAME); return(database); } catch (Exception ex) { throw new MB.Util.APPException(string.Format("创建数据库连接出错,请检查Provider {0}, ConnectionString {1} 配置是否正确:" + ex.Message, provider, connectionString), APPMessageType.SysErrInfo); } }
void System.Configuration.Internal.IInternalConfigSystem.RefreshConfig(string sectionName) { if (sectionName == ConnectionStringsSectionName) { this.connectionStringsSection = null; } internalConfigSystem.RefreshConfig(sectionName); }
/// <summary> /// retrieve the entire ConnectionStringsSection /// </summary> /// <param name="configurationSectionXpath"></param> /// <returns></returns> public static System.Configuration.ConnectionStringsSection getConnectionStringsSection(string configurationSectionXpath) { System.Configuration.ConnectionStringsSection result = (System.Configuration.ConnectionStringsSection) System.Configuration.ConfigurationManager.GetSection( configurationSectionXpath); if (null == result) { throw new System.Exception("la sezione richiesta non e' presente in configurazione."); }// else continue // return(result); }//end static getConnectionStringsSection
public DataProxy() { System.Configuration.Configuration exeConfiguration = System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Reflection.Assembly.GetEntryAssembly().Location); System.Configuration.ConnectionStringsSection connectionStringsSection = exeConfiguration.Sections.OfType <System.Configuration.ConnectionStringsSection>().Single(); Configuration configuration = new Configuration(); configuration.Configure(GetType().Assembly, "TaxiOnline.Server.Data.hibernate.cfg.xml"); configuration.Properties.Add("connection.connection_string", connectionStringsSection.ConnectionStrings["DefaultConnectionString"].ConnectionString); configuration.AddAssembly(GetType().Assembly); ISessionFactory sessionFactory = configuration.BuildSessionFactory(); _session = sessionFactory.OpenSession(); }
public void EditSchemaOnBuilding() { System.Configuration.Configuration objConfig = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~"); System.Configuration.ConnectionStringsSection objSectionSettings = (System.Configuration.ConnectionStringsSection)objConfig.GetSection("connectionStrings"); var conn = string.Format("DATA SOURCE = {0}; PASSWORD= {1}; USER ID={2};", DatabaseName, Pass, User_Id); //Edit if (objSectionSettings != null && !(objSectionSettings.ConnectionStrings["mysqlconnection"].ConnectionString == conn)) { objSectionSettings.ConnectionStrings["mysqlconnection"].ConnectionString = conn; objSectionSettings.ConnectionStrings["DbLogErrorContext"].ConnectionString = conn; objConfig.Save(); } }
System.Configuration.ConnectionStringsSection CreateConnectionStringsSection() { var internalConnectionStringsSection = (System.Configuration.ConnectionStringsSection)internalConfigSystem.GetSection(ConnectionStringsSectionName); var connectionStringsSection = new System.Configuration.ConnectionStringsSection(); foreach (var connectionStringSettings in internalConnectionStringsSection.ConnectionStrings.Cast <System.Configuration.ConnectionStringSettings>()) { var patchedConnectionStringSettings = new System.Configuration.ConnectionStringSettings( connectionStringSettings.Name, DbEngineDetector.PatchConnectionString(connectionStringSettings.ConnectionString, connectionStringSettings.ProviderName == EntityFrameworkConnectionStringProviderName), connectionStringSettings.ProviderName ); connectionStringsSection.ConnectionStrings.Add(patchedConnectionStringSettings); } return(connectionStringsSection); }
/// <summary>Creates a new instance of the <see cref="ConfigurationBuilder"/>.</summary> public ConfigurationBuilder(IDefinitionProvider[] definitionProviders, ClassMappingGenerator generator, IWebContext webContext, ConfigurationBuilderParticipator[] participators, DatabaseSection config, ConnectionStringsSection connectionStrings) { this.definitionProviders = definitionProviders; this.generator = generator; this.webContext = webContext; this.participators = participators; if (config == null) config = new DatabaseSection(); if (!string.IsNullOrEmpty(config.HibernateMapping)) DefaultMapping = config.HibernateMapping; SetupProperties(config, connectionStrings); SetupMappings(config); TryLocatingHbmResources = config.TryLocatingHbmResources; tablePrefix = config.TablePrefix; batchSize = config.BatchSize; childrenLaziness = config.ChildrenLaziness; }
private void UpdateConnectionStrings(string section, System.Configuration.ConfigurationSection configurationSection, System.Configuration.Configuration config, string protectionProvider) { System.Configuration.ConnectionStringsSection current = config.ConnectionStrings; if (current == null) { config.Sections.Add(section, configurationSection); ProtectConfigurationSection(configurationSection, protectionProvider); } else { System.Configuration.ConnectionStringsSection newConnectionStrings = (System.Configuration.ConnectionStringsSection)configurationSection; foreach (System.Configuration.ConnectionStringSettings connectionString in newConnectionStrings.ConnectionStrings) { if (current.ConnectionStrings[connectionString.Name] == null) { current.ConnectionStrings.Add(connectionString); } } ProtectConfigurationSection(current, protectionProvider); } }
/// <summary>Sets properties configuration dictionary based on configuration in the database section.</summary> /// <param name="config">The database section configuration.</param> /// <param name="connectionStrings">Connection strings from configuration</param> protected void SetupProperties(DatabaseSection config, ConnectionStringsSection connectionStrings) { NHibernate.Cfg.Environment.UseReflectionOptimizer = Utility.GetTrustLevel() > System.Web.AspNetHostingPermissionLevel.Medium; // connection Properties[NHibernate.Cfg.Environment.ConnectionStringName] = config.ConnectionStringName; Properties[NHibernate.Cfg.Environment.ConnectionProvider] = "NHibernate.Connection.DriverConnectionProvider"; Properties[NHibernate.Cfg.Environment.Hbm2ddlKeyWords] = "none"; SetupFlavourProperties(config, connectionStrings); SetupCacheProperties(config); // custom config properties foreach (string key in config.HibernateProperties.AllKeys) { Properties[key] = config.HibernateProperties[key].Value; } }
public static System.Data.SqlClient.SqlConnection connectWithPreparedString( string connectionStringName) { System.Data.SqlClient.SqlConnection result = new System.Data.SqlClient.SqlConnection(); try { // the following lines throw if required connection string is absent in "connectionStrings" System.Configuration.ConnectionStringsSection connectionStringsSection = ConfigurationLayer.ConfigurationService.getConnectionStringsSection( "connectionStrings");// this section name is compulsory result.ConnectionString = ConfigurationLayer.ConfigurationService.getSingleConnectionStringInSection( connectionStringsSection, connectionStringName); // try open result.Open();// returns null on non-opened connection } catch (System.Exception ex) { string error = ex.Message; return(null); } return(result); }// end connectWithPreparedString
}//end static getConnectionStringsSection /// <summary> /// retrieve the specified connectionStringName in the ConnectionStringsSection /// </summary> /// <param name="connectionStringsSection"></param> /// <param name="connectionStringName"></param> /// <returns></returns> public static string getSingleConnectionStringInSection( System.Configuration.ConnectionStringsSection connectionStringsSection, string connectionStringName) {// returns null on wrong connectionStringName return(connectionStringsSection.ConnectionStrings[connectionStringName].ConnectionString); }// end getSingleConnectionStringInSection
private void SetupFlavourProperties(DatabaseSection config, ConnectionStringsSection connectionStrings) { DatabaseFlavour flavour = config.Flavour; if (flavour == DatabaseFlavour.AutoDetect) { ConnectionStringSettings css = connectionStrings.ConnectionStrings[config.ConnectionStringName]; if (css == null) throw new ConfigurationErrorsException("Could not find the connection string named '" + config.ConnectionStringName + "' that was defined in the n2/database configuration section."); flavour = DetectFlavor(css); } // HACK: used to support seamless text/nvarchar(max) support across databases if (flavour == DatabaseFlavour.MySql) stringLength = 16777215; switch (flavour) { case DatabaseFlavour.SqlServer2000: Properties[NHibernate.Cfg.Environment.ConnectionDriver] = typeof(NHibernate.Driver.SqlClientDriver).AssemblyQualifiedName; Properties[NHibernate.Cfg.Environment.Dialect] = typeof(NHibernate.Dialect.MsSql2000Dialect).AssemblyQualifiedName; break; case DatabaseFlavour.SqlServer: case DatabaseFlavour.SqlServer2005: Properties[NHibernate.Cfg.Environment.ConnectionDriver] = typeof(NHibernate.Driver.SqlClientDriver).AssemblyQualifiedName; Properties[NHibernate.Cfg.Environment.Dialect] = typeof(NHibernate.Dialect.MsSql2005Dialect).AssemblyQualifiedName; break; case DatabaseFlavour.SqlServer2008: Properties[NHibernate.Cfg.Environment.ConnectionDriver] = typeof(NHibernate.Driver.SqlClientDriver).AssemblyQualifiedName; Properties[NHibernate.Cfg.Environment.Dialect] = typeof(NHibernate.Dialect.MsSql2008Dialect).AssemblyQualifiedName; break; case DatabaseFlavour.SqlCe3: Properties[NHibernate.Cfg.Environment.ConnectionDriver] = typeof(NHibernate.Driver.SqlServerCeDriver).AssemblyQualifiedName; Properties[NHibernate.Cfg.Environment.Dialect] = typeof(NHibernate.Dialect.MsSqlCeDialect).AssemblyQualifiedName; break; case DatabaseFlavour.SqlCe: case DatabaseFlavour.SqlCe4: Properties[NHibernate.Cfg.Environment.ConnectionDriver] = typeof(NHibernate.Driver.SqlServerCeDriver).AssemblyQualifiedName; Properties[NHibernate.Cfg.Environment.Dialect] = typeof(NHibernate.Dialect.MsSqlCe40Dialect).AssemblyQualifiedName; break; case DatabaseFlavour.MySql: Properties[NHibernate.Cfg.Environment.ConnectionDriver] = typeof(NHibernate.Driver.MySqlDataDriver).AssemblyQualifiedName; Properties[NHibernate.Cfg.Environment.Dialect] = typeof(NHibernate.Dialect.MySQL5Dialect).AssemblyQualifiedName; break; case DatabaseFlavour.SqLite: Properties[NHibernate.Cfg.Environment.ConnectionDriver] = typeof(NHibernate.Driver.SQLite20Driver).AssemblyQualifiedName; Properties[NHibernate.Cfg.Environment.Dialect] = typeof(NHibernate.Dialect.SQLiteDialect).AssemblyQualifiedName; break; case DatabaseFlavour.Firebird: Properties[NHibernate.Cfg.Environment.ConnectionDriver] = typeof(NHibernate.Driver.FirebirdDriver).AssemblyQualifiedName; Properties[NHibernate.Cfg.Environment.Dialect] = typeof(NHibernate.Dialect.FirebirdDialect).AssemblyQualifiedName; break; case DatabaseFlavour.Generic: Properties[NHibernate.Cfg.Environment.ConnectionDriver] = typeof(NHibernate.Driver.OleDbDriver).AssemblyQualifiedName; Properties[NHibernate.Cfg.Environment.Dialect] = typeof(NHibernate.Dialect.GenericDialect).AssemblyQualifiedName; break; case DatabaseFlavour.Jet: Properties[NHibernate.Cfg.Environment.ConnectionDriver] = "NHibernate.JetDriver.JetDriver, NHibernate.JetDriver"; Properties[NHibernate.Cfg.Environment.Dialect] = "NHibernate.JetDriver.JetDialect, NHibernate.JetDriver"; break; case DatabaseFlavour.DB2: Properties[NHibernate.Cfg.Environment.ConnectionDriver] = typeof(NHibernate.Driver.OdbcDriver).AssemblyQualifiedName; Properties[NHibernate.Cfg.Environment.Dialect] = typeof(NHibernate.Dialect.DB2Dialect).AssemblyQualifiedName; break; case DatabaseFlavour.Oracle9i: Properties[NHibernate.Cfg.Environment.ConnectionDriver] = typeof(NHibernate.Driver.OracleClientDriver).AssemblyQualifiedName; Properties[NHibernate.Cfg.Environment.Dialect] = typeof(NHibernate.Dialect.Oracle9iDialect).AssemblyQualifiedName; break; case DatabaseFlavour.Oracle: case DatabaseFlavour.Oracle10g: Properties[NHibernate.Cfg.Environment.ConnectionDriver] = typeof(NHibernate.Driver.OracleClientDriver).AssemblyQualifiedName; Properties[NHibernate.Cfg.Environment.Dialect] = typeof(NHibernate.Dialect.Oracle10gDialect).AssemblyQualifiedName; break; default: throw new ConfigurationErrorsException("Couldn't determine database flavour. Please check the 'flavour' attribute of the n2/database configuration section."); } }
/// <summary>Creates a new instance of the <see cref="ConfigurationBuilder"/>.</summary> public ConfigurationBuilder(IDefinitionProvider[] definitionProviders, ClassMappingGenerator generator, IWebContext webContext, ConfigurationBuilderParticipator[] participators, DatabaseSection config, ConnectionStringsSection connectionStrings) { this.definitionProviders = definitionProviders; this.generator = generator; this.webContext = webContext; this.participators = participators; if (config == null) { config = new DatabaseSection(); } if (!string.IsNullOrEmpty(config.HibernateMapping)) { DefaultMapping = config.HibernateMapping; } SetupProperties(config, connectionStrings); SetupMappings(config); TryLocatingHbmResources = config.TryLocatingHbmResources; tablePrefix = config.TablePrefix; batchSize = config.BatchSize; childrenLaziness = config.ChildrenLaziness; }
private void SetupFlavourProperties(DatabaseSection config, ConnectionStringsSection connectionStrings) { DatabaseFlavour flavour = config.Flavour; if (flavour == DatabaseFlavour.AutoDetect) { ConnectionStringSettings css = connectionStrings.ConnectionStrings[config.ConnectionStringName]; if (css == null) { throw new ConfigurationErrorsException("Could not find the connection string named '" + config.ConnectionStringName + "' that was defined in the n2/database configuration section."); } flavour = DetectFlavor(css); } // HACK: used to support seamless text/nvarchar(max) support across databases if (flavour == DatabaseFlavour.MySql) { stringLength = 16777215; } switch (flavour) { case DatabaseFlavour.SqlServer2000: Properties[NHibernate.Cfg.Environment.ConnectionDriver] = typeof(NHibernate.Driver.SqlClientDriver).AssemblyQualifiedName; Properties[NHibernate.Cfg.Environment.Dialect] = typeof(NHibernate.Dialect.MsSql2000Dialect).AssemblyQualifiedName; break; case DatabaseFlavour.SqlServer: case DatabaseFlavour.SqlServer2005: Properties[NHibernate.Cfg.Environment.ConnectionDriver] = typeof(NHibernate.Driver.SqlClientDriver).AssemblyQualifiedName; Properties[NHibernate.Cfg.Environment.Dialect] = typeof(NHibernate.Dialect.MsSql2005Dialect).AssemblyQualifiedName; break; case DatabaseFlavour.SqlServer2008: Properties[NHibernate.Cfg.Environment.ConnectionDriver] = typeof(NHibernate.Driver.SqlClientDriver).AssemblyQualifiedName; Properties[NHibernate.Cfg.Environment.Dialect] = typeof(NHibernate.Dialect.MsSql2008Dialect).AssemblyQualifiedName; break; case DatabaseFlavour.SqlCe3: Properties[NHibernate.Cfg.Environment.ConnectionDriver] = typeof(NHibernate.Driver.SqlServerCeDriver).AssemblyQualifiedName; Properties[NHibernate.Cfg.Environment.Dialect] = typeof(NHibernate.Dialect.MsSqlCeDialect).AssemblyQualifiedName; break; case DatabaseFlavour.SqlCe: case DatabaseFlavour.SqlCe4: Properties[NHibernate.Cfg.Environment.ConnectionDriver] = typeof(NHibernate.Driver.SqlServerCeDriver).AssemblyQualifiedName; Properties[NHibernate.Cfg.Environment.Dialect] = typeof(NHibernate.Dialect.MsSqlCe40Dialect).AssemblyQualifiedName; break; case DatabaseFlavour.MySql: Properties[NHibernate.Cfg.Environment.ConnectionDriver] = typeof(NHibernate.Driver.MySqlDataDriver).AssemblyQualifiedName; Properties[NHibernate.Cfg.Environment.Dialect] = typeof(NHibernate.Dialect.MySQL5Dialect).AssemblyQualifiedName; break; case DatabaseFlavour.SqLite: Properties[NHibernate.Cfg.Environment.ConnectionDriver] = typeof(NHibernate.Driver.SQLite20Driver).AssemblyQualifiedName; Properties[NHibernate.Cfg.Environment.Dialect] = typeof(NHibernate.Dialect.SQLiteDialect).AssemblyQualifiedName; break; case DatabaseFlavour.Firebird: Properties[NHibernate.Cfg.Environment.ConnectionDriver] = typeof(NHibernate.Driver.FirebirdDriver).AssemblyQualifiedName; Properties[NHibernate.Cfg.Environment.Dialect] = typeof(NHibernate.Dialect.FirebirdDialect).AssemblyQualifiedName; break; case DatabaseFlavour.Generic: Properties[NHibernate.Cfg.Environment.ConnectionDriver] = typeof(NHibernate.Driver.OleDbDriver).AssemblyQualifiedName; Properties[NHibernate.Cfg.Environment.Dialect] = typeof(NHibernate.Dialect.GenericDialect).AssemblyQualifiedName; break; case DatabaseFlavour.Jet: Properties[NHibernate.Cfg.Environment.ConnectionDriver] = "NHibernate.JetDriver.JetDriver, NHibernate.JetDriver"; Properties[NHibernate.Cfg.Environment.Dialect] = "NHibernate.JetDriver.JetDialect, NHibernate.JetDriver"; break; case DatabaseFlavour.DB2: Properties[NHibernate.Cfg.Environment.ConnectionDriver] = typeof(NHibernate.Driver.OdbcDriver).AssemblyQualifiedName; Properties[NHibernate.Cfg.Environment.Dialect] = typeof(NHibernate.Dialect.DB2Dialect).AssemblyQualifiedName; break; case DatabaseFlavour.Oracle9i: Properties[NHibernate.Cfg.Environment.ConnectionDriver] = typeof(NHibernate.Driver.OracleClientDriver).AssemblyQualifiedName; Properties[NHibernate.Cfg.Environment.Dialect] = typeof(NHibernate.Dialect.Oracle9iDialect).AssemblyQualifiedName; break; case DatabaseFlavour.Oracle: case DatabaseFlavour.Oracle10g: Properties[NHibernate.Cfg.Environment.ConnectionDriver] = typeof(NHibernate.Driver.OracleClientDriver).AssemblyQualifiedName; Properties[NHibernate.Cfg.Environment.Dialect] = typeof(NHibernate.Dialect.Oracle10gDialect).AssemblyQualifiedName; break; default: throw new ConfigurationErrorsException("Couldn't determine database flavour. Please check the 'flavour' attribute of the n2/database configuration section."); } }
void System.Configuration.Internal.IInternalConfigSystem.RefreshConfig(string sectionName) { if(sectionName == ConnectionStringsSectionName) this.connectionStringsSection = null; internalConfigSystem.RefreshConfig(sectionName); }
System.Configuration.ConnectionStringsSection CreateConnectionStringsSection() { var internalConnectionStringsSection = (System.Configuration.ConnectionStringsSection)internalConfigSystem.GetSection(ConnectionStringsSectionName); var connectionStringsSection = new System.Configuration.ConnectionStringsSection(); foreach(var connectionStringSettings in internalConnectionStringsSection.ConnectionStrings.Cast<System.Configuration.ConnectionStringSettings>()) { var patchedConnectionStringSettings = new System.Configuration.ConnectionStringSettings( connectionStringSettings.Name, DbEngineDetector.PatchConnectionString(connectionStringSettings.ConnectionString, connectionStringSettings.ProviderName == EntityFrameworkConnectionStringProviderName), connectionStringSettings.ProviderName ); connectionStringsSection.ConnectionStrings.Add(patchedConnectionStringSettings); } return connectionStringsSection; }
///<summary> ///加密配置文件中的ConnectionString节 ///</summary> ///<param name="protect">true为加密,false为解密</param> public static void ConnectionStringProtection(bool protect) { //取得当前程序的执行路径 string pathName = Application.ExecutablePath; // 定义Dpapi提供程序的名称. string strProvider = "DataProtectionConfigurationProvider"; System.Configuration.Configuration oConfiguration = null; System.Configuration.ConnectionStringsSection oSection = null; try { // 打开配置文件,并取得connectionStrings配置节. oConfiguration = System.Configuration.ConfigurationManager.OpenExeConfiguration(pathName); if (oConfiguration != null) { bool blnChanged = false; oSection = oConfiguration.GetSection("connectionStrings") as System.Configuration.ConnectionStringsSection; if (oSection != null) { if ((!(oSection.ElementInformation.IsLocked)) && (!(oSection.SectionInformation.IsLocked))) { if (protect) { if (!(oSection.SectionInformation.IsProtected)) { blnChanged = true; // 加密connectionStrings配置节. oSection.SectionInformation.ProtectSection(strProvider); } } else { if (oSection.SectionInformation.IsProtected) { blnChanged = true; // 解密connectionStrings配置节. oSection.SectionInformation.UnprotectSection(); } } } if (blnChanged) { // 如果connectionStrings配置节被更改,则强制保存它. oSection.SectionInformation.ForceSave = true; // 保存对connectionStrings配置节的更改. oConfiguration.Save(); } } } } catch (System.Exception ex) { throw (ex); } finally { } }
/// <summary>Sets properties configuration dictionary based on configuration in the database section.</summary> /// <param name="config">The database section configuration.</param> /// <param name="connectionStrings">Connection strings from configuration</param> protected void SetupProperties(DatabaseSection config, ConnectionStringsSection connectionStrings) { NHibernate.Cfg.Environment.UseReflectionOptimizer = Utility.GetTrustLevel() > System.Web.AspNetHostingPermissionLevel.Medium; // connection Properties[NHibernate.Cfg.Environment.ConnectionStringName] = config.ConnectionStringName; Properties[NHibernate.Cfg.Environment.ConnectionProvider] = "NHibernate.Connection.DriverConnectionProvider"; Properties[NHibernate.Cfg.Environment.Hbm2ddlKeyWords] = "none"; SetupFlavourProperties(config, connectionStrings); SetupCacheProperties(config); // custom config properties foreach (string key in config.HibernateProperties.AllKeys) { Properties[key] = config.HibernateProperties[key].Value; } }