Changing connection string at runtime in Enterprise Library

后端 未结 4 572
星月不相逢
星月不相逢 2020-12-16 04:33

Is there a way to change the connection string of a DataBase object in Enterprise Library at runtime? I\'ve found this link but its a little bit outdated (2005)

I\'v

相关标签:
4条回答
  • 2020-12-16 04:41

    look at this:Open Microsoft.practices.EnterpriseLibrary database with just a connection string

    just use this follow code, you can programming create database at runtime

    database mydb = new EnterpriseLibrary.Data.Sql.SqlDatabase("connection string here");
    

    It's solved my problem. I have a single web app using many database, according to the different subdomain in the url to connect to the different database. such as:

    • abc.test.com ------>using the Db_projectABC
    • def.test.com ------>using the db_ProjectDEF

    I use url-rewrite to parse the subdomain name, and use the subdomain name to chose it's database connection string which stored in the main database.

    Thanks

    0 讨论(0)
  • 2020-12-16 04:45

    Here's from Yang's Net Zone:

    using Microsoft.Practices.EnterpriseLibrary.Data;
    using Microsoft.Practices.EnterpriseLibrary.Configuration;
    using Microsoft.Practices.EnterpriseLibrary.Data.Configuration;
    
    DatabaseSettings settings = new DatabaseSettings();
    
    // This maps to <databaseType> element in data config file
    DatabaseTypeData type = new DatabaseTypeData("Sql Server", "Microsoft.Practices.EnterpriseLibrary.Data.Sql.SqlDatabase, Microsoft.Practices.EnterpriseLibrary.Data, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null");
    settings.DatabaseTypes.Add(type);
    
    // This maps to <connectionString> element in data config file
    ConnectionStringData connectionString = new ConnectionStringData("localhost.EntLibQuickStarts");
    
    // Followings map to <parameter> elements in data config file
    ParameterData param = new ParameterData("server", "localhost");
    connectionString.Parameters.Add(param);
    
    param = new ParameterData("database", "EntLibQuickStarts");
    connectionString.Parameters.Add(param);
    
    param = new ParameterData("integrated security", "true");
    connectionString.Parameters.Add(param);
    
    settings.ConnectionStrings.Add(connectionString);
    
    // Too bad compiler gets confused InstanceData with System.Diagnostics.InstanceData.  It maps to <instance> element in data config file
    Microsoft.Practices.EnterpriseLibrary.Data.Configuration.InstanceData instance = new    Microsoft.Practices.EnterpriseLibrary.Data.Configuration.InstanceData("localhost", "Sql Server", "localhost.EntLibQuickStarts");
    settings.Instances.Add(instance);
    
    ConfigurationDictionary configurations = new ConfigurationDictionary();
    
    // This is how to tie DatabaseSettings with ConfigurationDictionary. It maps to <configurationSection name="dataConfiguration"> element in App.config file    configurations.Add("dataConfiguration", settings);
    ConfigurationContext context = ConfigurationManager.CreateContext(configurations);
    
    Database database = new DatabaseProviderFactory(context).CreateDatabase("localhost");
    
    0 讨论(0)
  • 2020-12-16 04:47

    We can use the following code snippet to connect to multiple databases.

    DLLs to Add as Reference

    1. Microsoft.Practices.EnterpriseLibrary.Common.dll
    2. Microsoft.Practices.EnterpriseLibrary.Data.dll
    3. Microsoft.Practices.ServiceLocation.dll

    The snippet:

    var builder = new ConfigurationSourceBuilder();
    
            builder.ConfigureData()
                   .ForDatabaseNamed("LocalSqlServer1")
                     .ThatIs.ASqlDatabase()
                     .WithConnectionString(@"Data Source=PCNAME\SQLEXPRESS;Initial Catalog=ContactDB;Integrated Security=True")
                   .ForDatabaseNamed("LocalSqlServer2")
                     .ThatIs.ASqlDatabase()
                     .WithConnectionString(@"Data Source=PCNAME\SQLEXPRESS;Initial Catalog=MyDB;Integrated Security=True");
    
            var configSource = new DictionaryConfigurationSource();
            builder.UpdateConfigurationWithReplace(configSource);
    
    Microsoft.Practices.EnterpriseLibrary.Common.Configuration.EnterpriseLibraryContainer.Current = EnterpriseLibraryContainer.CreateDefaultContainer(configSource);      
    
    Database destinationDatabase = DatabaseFactory.CreateDatabase("LocalSqlServer2");      
    
    0 讨论(0)
  • 2020-12-16 04:56

    If you take a look at "Enterprise Library Docs - Adding Application Code" it says this:

    "If you know the connection string for the database you want to create, you can bypass the application's configuration information and use a constructor to directly create the Database object. Because the Database class is an abstract base class, you must construct one of its derived types. The derived Database type determines the ADO.NET data provider. For example, the SqlDatabase class uses the SqlClientFactory provider, the SqlCeDatabase class uses the SqlCeProviderFactory provider, and the OracleDatabase class uses the OracleClientFactory provider. It is your responsibility to construct the appropriate type of Database class for the connection string."

    It then goes on to give some examples. This would suggest that you should not be using the DatabaseFactory and you should be creating a new Database class for each of your different connections.

    0 讨论(0)
提交回复
热议问题