Reputation: 259
Why do we store connection strings in web.config file? What are the benefits of doing so?
Upvotes: 2
Views: 3079
Reputation: 6825
What I like most about having the connection string in the web.config is when you have multiple environments that you test on. Say you have a Test server, Staging server and a Production server. Well you don't need to have code that checks which server you're on, or have to recompile different versions for each... just use the web.config to store your connection strings and that way you can have a different web.config on each server but the same web application with no hassles. You may want to Encrypt your Connection String Settings as well so they're not visible to everyone that has access to the folder.
Upvotes: 1
Reputation: 1799
Reason #1
As everyone is mentioning, having the connection string in the web.config makes it easy to update/change as needed. It becomes a single source where the arguments can be easily be changed.
Reason #2
Beyond that though, IIS is configured not serve up web.config to users who request the file. If your website is,
www.mydomain.com
someone can't hit http://www.mydomain.com/web.config and scrape all your confidential settings, passwords, and so forth.
(As a side, note, IIS won't serve up files in the App_Code directory either to a user, so the web.config file isn't unique in this respect.)
Reason #3
ASP.NET automatically detects changes to configuration files and will immediately applies new settings.
More info..
MSDN has a discussion of the ASP.NET configuration system at,
http://msdn.microsoft.com/en-us/library/aa719558%28VS.71%29.aspx
Upvotes: 2
Reputation: 57996
Imagine you have several classes which access your database; you can:
These have following characteristics:
So, by storing a connection string into your web.config
file you gain more flexibility to change that setting than other alternatives.
Upvotes: 3
Reputation: 33183
The web config file is used so you can reference the connection anywhere in your app. It avoids having to deal with a fairly long connection string within your application.
Here is a good article which may give you detailed information: http://articles.sitepoint.com/article/web-config-file-demystified#
There is even a wiki page for it :) (surprisingly): http://en.wikipedia.org/wiki/Web.config
If you ever move / change to a different database format the connection string setting inside of the web.config file can be changed in one place rather then digging through your entire application looking for it. This also avoids having to recompile or build an application to get the new connection string setting.
If you are wondering how to access the information from within a web.config file that can be found here:
http://msdn.microsoft.com/en-us/library/4c2kcht0(VS.85).aspx
There is also sample code right in there.
Upvotes: 6
Reputation: 37234
You don't have to re-build the application if the connection string changes. An admin can make the change and the code remains the same.
Upvotes: 0
Reputation: 3126
It allows the connection string to be configured by someone maintaining the site.
Upvotes: 0
Reputation: 191058
You can reference them using the ConfigurationManager
via the ConnectionStrings
property.
Upvotes: 0