Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Can We Have two Connection Strings In Web.Config And Switch Betweeen Them In Code Behind?

When I add two connection strings in the web.config, an error appears that tells me I can't add two connection strings in the web.config.

I want the upper job because I have 2 databases and I want transfer data from another to the other one.

Would you please show me a way for doing that?

like image 819
LostLord Avatar asked Nov 19 '10 17:11

LostLord


People also ask

How do you change a connection string?

To edit a connection string stored in application settingsLocate the connection you want to edit and select the text in the Value field. Edit the connection string in the Value field, or click the ellipsis (...) button in the Value field to edit your connection with the Connection Properties dialog box.

Where do we set connection string in web config?

Declaring connectionStrings in web. config file: <connectionStrings> <add name="dbconnection" connectionString="Data Source=Soumalya;Integrated Security=true;Initial Catalog=MySampleDB" providerName="System.

Is it safe to store connection string in web config?

The connection strings are mostly stored in web. config. It means that connection specific information such as database name, username, and password are stored as a clear text in a file. This is definitely a security concern for your Production servers.


1 Answers

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;
like image 98
Darin Dimitrov Avatar answered Sep 17 '22 12:09

Darin Dimitrov