It looks like you need to dynamically set the database name and connection string depending on the type of query. Even if you have, say (for example, 60) Databases that you can connect to using hard-coded statements, this is something that no one ever recommends because
- Hard to write
- Hard to manage
- Hard to upgrade
- You can use a separate code file for that, but it's kind of reinventing the wheel.
The configuration files serve this purpose along with many others and think about your colleagues for a second, looking at this β CustomConnectionStringsFile β and scratching their head.
The best way is to save them as connectionStrings in the configuration file and use the one you need
Something like that
Adding a Web.Config Connection String
<connectionStrings> <add name="NorthwindConnectionString" connectionString="Data Source=serverName;Initial Catalog=Northwind;Persist Security Info=True;User ID=userName;Password=password" providerName="System.Data.SqlClient" /> <add name="BestDBConnectionString" connectionString="Data Source=serverName;Initial Catalog= BestDB;Persist Security Info=True;User ID=userName;Password=password" providerName="System.Data.SqlClient" /> </connectionStrings>
Connector Access
string myConnString =""; if(ThisIsThat("A")) { myConnString = rootWebConfig.ConnectionStrings.ConnectionStrings["NorthwindConnectionString"]; } else if(ThisIsThat("B")) { myConnString = rootWebConfig.ConnectionStrings.ConnectionStrings["BestDBConnectionString"] } { else
For more information about
Hope this helps
source share