locked
EntLib Connectionstring Runtime using DAAB RRS feed

  • Question

  • User905623933 posted

    My app needs to support multiple SQL server databases (DB1, DB2, DB3....) on different physical servers Server 1 and Server 2. How can I configure the web.config and create the database connection string at runtime when I comet to know the database. I am using data access application block. Do I need to create different <instances> and <connectionStrings> to every instance ? How do I make a connection with the instances mentioned in the web.config in C# ??

    Thanks in advance
    marcus

     

     

    Saturday, December 15, 2007 12:09 PM

All replies

  • User-168033412 posted

    Hi,

    We are using similar techniques on most of our projects. If you are using .Net 2.0, then you can define multiple connection strings in it with different name. Lets say with each computer name a connection string is associated.

    When you create a database instance using DAAB, you have to pass an instance name which actually is your connection string name. Here you can implement a check which first gets current machine name and then pass that as instance name parameter to create database call.

     

     

    //DAAB Logic
    
    DatabaseFactory.CreateDatabase(Environment.MachineName);
      

     

     

    /* Connection strings */
    
    <add name="SERVER1" connectionString="" providerName="System.Data.SqlClient"/>
    <add name="SERVER2" connectionString="" providerName="System.Data.SqlClient"/>
    
      
    Monday, December 17, 2007 2:19 AM
  • User905623933 posted

    Thank you for you answer. However it doesn't solve my purpose. Each of the servers have approx 200 databases. With this approach I will have to have 200 + 200 connection string in web.config.
    Is it possible to use the EntLib function CreateDatabase and specify the connection string there ? I can pass the connection string from the UI to the DAL.

    Thank you in advance. 

     

    Monday, December 17, 2007 12:56 PM
  • User-550857255 posted

     You can add the connection string to your configuration manager like this:       

     

            /// <summary>
            /// Adds the connection string.
            /// </summary>
            /// <Author>jarchambault</Author>
            /// <Date>14/04/2008</Date>
            private static void AddConnectionString()
            {
                // Get the application configuration file.
                System.Configuration.Configuration config =
                        ConfigurationManager.OpenExeConfiguration(
                        ConfigurationUserLevel.None);
    
                // Get the conectionStrings section.
                ConnectionStringsSection csSection =
                    config.ConnectionStrings;
    
                //Create your connection string into a connectionStringSettings object
                ConnectionStringSettings connection = CreateNewConnectionString();
    
                //Add the object to the configuration
                csSection.ConnectionStrings.Add(connection);
    
                //Save the configuration
                config.Save(ConfigurationSaveMode.Modified);
    
                //Refresh the Section
                ConfigurationManager.RefreshSection("connectionStrings");
            } 
     
    Then the DAAB will be able to read your newly added connection string.
     
    Hope it helps.
     
    -joel 
     

     

    Monday, April 14, 2008 8:11 AM