I have a dropdown that displays all the databases in the Server. So When I click on some database It should use the particular database for the whole application. What is the best method to do that?
Should I create the connectionString
dynamically or just add multiple ConnectionStrings
in Web.Config
?
This is more of an implementation technique.
In your case I am assuming the following:
1) The dropdown is filled dynamically from the available Db's in the database.
2) The connection to the server remains constant, only the db name needs to change.
If the above is true, you can store a template connection string in the config, e.g.:
<connectionStrings>
<add name="sampleConn" connectionString="Data Source=<yourServer>;Initial Catalog={0};Integrated Security=true;" providerName="System.Data.SqlClient"/>
...
And then you can read in the code like:
var conString = string.Format(ConfigurationManager.ConnectionStrings["sampleConn"].ConnectionString, drpValue);
where drpValue is the db name from the dropdown.