Different Databases for Different Environments RRS feed

  • Question

  • User938738290 posted

    I'm developing an ASP.NET Web Pages site for .NET 4 (Razor v2 syntax) in Visual Studio 2013 Express for Web.

    At the moment, I basically have two environments, "Development" on my local machine, and "Production" on the server.  I have the environments set to use different data sources via the following code in _AppStart.cshtml:

        string lConnectionStringName = null;
        if(ApplicationInstance.Server.MachineName == "WEBSERVERNAME")
            lConnectionStringName = "ProductionConnectionString";
            lConnectionStringName = "DevelopmentConnectionString";
        WebSecurity.InitializeDatabaseConnection(lConnectionStringName, "USER_MASTER", "USER_ID", "USERNAME");

    If the app detects that it's running on the server, it uses the Production DB.  Otherwise, it uses the Dev DB.

    Now, I want to add a "Test" environment that would also run on the server, but use the Dev data source.

    Is there any way I can configure Visual Studio's build/deployment process to select which connection string in web.config to use, and/or alter the contents of a connection string?  Failing that, what other options do I have for choosing different data sources?

    Friday, May 9, 2014 9:56 AM


  • User1558924997 posted

    You can add Multiple connection strings into web.config having KEY as your base URL; e.g.

    		<add name="" connectionString="Data Source=prod;user id=root;password=l0cked1234;database=prodDB;" providerName="MySql.Data.MySqlClient" />
    <add name="http://localhost/" connectionString="Data Source=dev;user id=root;password=l0cked1234;database=smdev6;" providerName="MySql.Data.MySqlClient" />
    <add name="" connectionString="Data Source=dev;user id=root;password=l0cked1234;database=smdev6;" providerName="MySql.Data.MySqlClient" />

    Then use app URL as Key to pick the connection string. ConnectionKey can be added as an attribute to UserClass or your global object at user session level.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 9, 2014 10:18 AM