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.ServerVariables ["SERVER_NAME"]); } } internal string getConnectionStringByServerName (string serverName) { if (serverName.Equals("localhost")) { return WebConfigurationManager.ConnectionStrings ["DevelopmentDB"].ConnectionString ; } else { return WebConfigurationManager.ConnectionStrings ["ProductionDB"].ConnectionString ; } }
Comments
Post a Comment