locked
2 DBs for one web app RRS feed

  • Question

  • User630377037 posted

    Hello, I´m quite newbie with ASP.NET and I start working in an old project already in production. I have an ASP.NET web application deployed on a server, and I have the .dbml on the server side with the connection string to the Database that I´m using now. I would like to use a different one to do some tests while I´m developing. I have both connection strings to the DBs in web.config (also in server side) like this: 

    <add key="ActiveDataBase" value=" DB1_ConnectionString"/> <connectionStrings>
     <add name="DB1_ConnectionString" connectionString="Data Source=serverName;Initial Catalog=LTTechDB1_name;Persist Security Info=True;User ID=pepe;Password=****"
     <add name="DB2_ConnectionString" connectionString="Data Source=serverName;Initial Catalog=DB2_name;Persist Security Info=True;User ID=pepe;Password=****" providerName="System.Data.SqlClient" /> 
    </connectionStrings>

    I change the ActiveDataBase and the Connection in the Properties of the .dbml but it always uses the old DB. What am I doing wrong? Do I need to redeploy the app everytime I want to change the DB? Thanks a lot in advance,

    Monday, February 13, 2012 1:07 PM

Answers

  • User-718146471 posted

    Whiel I understand wanting to use a variable name for this, I think you are going to be better off doing it more like this:

    <connectionStrings>
     
      <add name="DB1_ConnectionString" connectionString="Data Source=serverName;Initial Catalog=DB1;Persist Security Info=True;User ID=pepe;Password=****" providerName="System.Data.SqlClient" />
     
     
    <!-- 
    Comment out the one you don't want 
    
    <add name="DB1_ConnectionString" connectionString="Data Source=serverName;Initial Catalog=DB2;Persist Security Info=True;User ID=pepe;Password=****" providerName="System.Data.SqlClient" />
    
    -->
     
    </connectionStrings>
    

     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 14, 2012 10:51 AM
  • User3866881 posted

    Hello germencita:)

    Besides the moderator's suggestion,if you are using LINQ-TO-SQL,you can also try to use this following thing——

    using (XXXLinqDataContext ldb = new XXXLinqDataContext(ConfigurationManager.ConnectionStrings["DB2_ConnectionString"].ConnectionString))
    {
        ………………

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 14, 2012 8:48 PM

All replies

  • User-718146471 posted

    What you can do is simply change the connectionstring in the web config. Uncomment the one you want to use and comment out the other but make sure they are named the same. Or keep two web config files and just rename them according to your needs.

    Monday, February 13, 2012 2:06 PM
  • User-718146471 posted

    It looks to me like you already have this in place so you simply edit your web config file to change connections.

    Monday, February 13, 2012 2:31 PM
  • User-718146471 posted

    Or you could build a small little Admin UI to change the active connection string. Store this in a little file (XML?) and have your web.config look at that file to determine which connection is active.

    Monday, February 13, 2012 2:32 PM
  • User630377037 posted

    Thanks a lot for you fast reply,

    I´ve tried to change the active DB by uncommenting just the one that I want to use, and commenting on the rest and I made sure that the one in dbml was the right one but it´s still not working.Cry

    Maybe I´m not doing things in the right order when I deploy. I do the following steps:

    1. Publish the app  locally (localhost)
    2. Copy the app from C:/Inetpub/wwwroot and paste it in the Server side in Visual Studio
    3. Publish the Web services in the server
    4. Update the Service reference
    5. Launch the web app in the browser.

    Is that Ok?

    Thanks a lot!!!

    Tuesday, February 14, 2012 10:11 AM
  • User-718146471 posted

    I just looked at your connection string stuff and somethign jumped out at me:

     

    <add key="ActiveDataBase" value=" DB1_ConnectionString"/> <connectionStrings> 
     <add name="DB1_ConnectionString" connectionString="Data Source=serverName;Initial Catalog=LTTechDB1_name;Persist Security Info=True;User ID=pepe;Password=****" 
    // where is the closing part of your tag??? // You have it for the second string but not first :) <add name="DB2_ConnectionString" connectionString="Data Source=serverName;Initial Catalog=DB2_name;Persist Security Info=True;User ID=pepe;Password=****" providerName="System.Data.SqlClient" /> </connectionStrings>
    Tuesday, February 14, 2012 10:24 AM
  • User-718146471 posted

    Also, the value of ActiveDataBase has a space, that should be removed. I trust you are calling out to the config variable ActiveDataBase from your code.

    Tuesday, February 14, 2012 10:25 AM
  • User630377037 posted

    Sorry, I copied it wrong but it was well in the real web.config.

    <appSettings>

        <add key="ActiveDataBase" value="DB2_ConnectionString"/>

    </appSettings>

    <connectionStrings>

      <add name="DB1_ConnectionString" connectionString="Data Source=serverName;Initial Catalog=DB1;Persist Security Info=True;User ID=pepe;Password=****" providerName="System.Data.SqlClient" />

     

    <add name="DB2_ConnectionString" connectionString="Data Source=serverName;Initial Catalog=DB2;Persist Security Info=True;User ID=pepe;Password=****" providerName="System.Data.SqlClient" />

    </connectionStrings>

    Tuesday, February 14, 2012 10:46 AM
  • User-718146471 posted

    Whiel I understand wanting to use a variable name for this, I think you are going to be better off doing it more like this:

    <connectionStrings>
     
      <add name="DB1_ConnectionString" connectionString="Data Source=serverName;Initial Catalog=DB1;Persist Security Info=True;User ID=pepe;Password=****" providerName="System.Data.SqlClient" />
     
     
    <!-- 
    Comment out the one you don't want 
    
    <add name="DB1_ConnectionString" connectionString="Data Source=serverName;Initial Catalog=DB2;Persist Security Info=True;User ID=pepe;Password=****" providerName="System.Data.SqlClient" />
    
    -->
     
    </connectionStrings>
    

     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 14, 2012 10:51 AM
  • User3866881 posted

    Hello germencita:)

    Besides the moderator's suggestion,if you are using LINQ-TO-SQL,you can also try to use this following thing——

    using (XXXLinqDataContext ldb = new XXXLinqDataContext(ConfigurationManager.ConnectionStrings["DB2_ConnectionString"].ConnectionString))
    {
        ………………

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 14, 2012 8:48 PM