Another Place to Put the Connection String

Another Place to Put the Connection String

by JBrooks 18. June 2009 09:44

Another way to store your connection string in a way that it doesn't get in the way when you promote your web site from Test to QA to Prod is the following:

Have your web site under a given directory, say d:\WebSites\MyWebsite.
And you also have another directory to hold your connection strings that isn't under that directory.   So I could have my connection string as an appSetting in the following file:
d:\WebSites\Support\config\WebEnvironment.config

The section looks like:
<appSettings>
    <add key="Env" value="Dev"/>
    <add key="DefaultConnection" value="server=MyServer;uid=MyUser;pwd=P@ssw0rd;database=MyDB;" />
</appSettings>

Then you have the following in your d:\WebSites\MyWebsite\web.config file

<appSettings file="../Support/config/WebEnvironment.config">
</appSettings>


You would retreive the string by:
System.Configuration.ConfigurationManager.AppSettings["DefaultConnection"]


-Things to note are that this now allows you to delete everything under d:\WebSites\MyWebsite before you redeploy your site.  
-If you change the connection string you need to do an IIS reset for the site to pick up the change.  
-You can't do the file="..." part with a connection

Tags:

ASP.Net | Development