How Can We Have two Connection Strings In Web.Config And Switch Betweeen Them In Code Behind?
When you add a connection string, you name it.
You can access each such connection string and assign it to a different variable, passing that connection string to your data access layer.
In the config file:
<connectionStrings>
<add name="Sales"
providerName="System.Data.SqlClient"
connectionString= "server=myserver;database=Products;uid=<user name>;pwd=<secure password>" />
<add name="NorthWind"
providerName="System.Data.SqlClient"
connectionString="server=.;database=NorthWind;Integrated Security=SSPI" />
</connectionStrings>
In your code:
var conn1 = ConfigurationManager.ConnectionStrings["Sales"].ConnectionString;
var conn2 = ConfigurationManager.ConnectionStrings["NorthWind"].ConnectionString;
Simply put those strings in your web.config:
<connectionStrings>
<add name="CS1"
connectionString="SOME CONNECTION STRING"
providerName="System.Data.SqlClient" />
<add name="CS2"
connectionString="SOME OTHER STRING"
providerName="System.Data.SqlClient" />
</connectionStrings>
And then pick the one you wish in your code:
string cs = ConfigurationManager.ConnectionStrings["CS2"].ConnectionString;