Like lots of people, I've wanted to store my ConnectionString in the web.config file. Again, like lots of people, I have different ConnectionStrings, one for the "live" server, one for the "development" server, and yet another for the "reporting" server. I'm sure many of you have the same thing. So then, you do development using the appropriate ConnectionString. Then when you deploy, how do you switch the ConnectionString to the new one? What I do, is in my web.config file I have quite a few parameters, not just ConnectionString. Like this:
LiveWebServerName, LiveServerConnectionString, DevServerConnectionString, ReportServerConnectionString, etc.
Then, in the application start code, I read those settings, and determine which ConnectionString to use. For example, if the LiveWebServerName is "SuperServer" and my application start code determines that it is indeed running on a box named "SuperServer" then it assigns the value from LiveServerConnectionString to be the "active connection string" for use throughout the site. Otherwise, it uses the DevServerConnectionString.
The strength of this, is that you don't have to remember to switch your web.config file, or have a second copy of the web.config, one for "live" and one for "dev." Or any tricks like that. This makes it foolproof. What is cool is that, no matter where you run your site from (your laptop, any team member's localhost, etc), it will only use the Development Server, never the Live Server. No chance for you to make a mistake and mess it up :) And of course once you deploy to your Live Web Server, it will automatically use the Live SQL Server.
| posted on Tuesday, November 18, 2003 8:38 PM