In your web.config file, you could define your two connection strings:
<add name="DevelopmentDB" providerName="System.Data. SqlClient"
connectionString="Data Source=sql-dev.example.com; Initial Catalog=MyDB;User Id=MyUser;Password=MyPassword" />
<add name="ProductionDB" providerName="System.Data. SqlClient"
connectionString="Data Source=sql-prod.example.com; Initial Catalog=MyDB;User Id=MyUser;Password=MyPassword" />
</connectionStrings>
Then, in your (base) controller, you could create a method that returns the appropriate connection string based upon the request, such as:
internal string ConnectionString { get { return getConnectionStringByServerNam
e(this.HttpContext.Request.Ser verVariables["SERVER_NAME"]); } } internal string getConnectionStringByServerNam e(string serverName) { if (serverName.Equals("localhost" )) { return WebConfigurationManager.Connec tionStrings["DevelopmentDB"].C onnectionString; } else { return WebConfigurationManager.Connec tionStrings["ProductionDB"].Co nnectionString; } }
Comments
Post a Comment