Benefits of using ConnectionStrings Element Instead of AppSettings
In .NET 1.0 we used ConfigurationSettings instead of ConfigurationManager
With .NET 2.0 we can use ConfigurationManager.ConnectionStrings, and gain 3 benefits. Actually, if you just did a search and replace and changed ConfigurationSettings.AppSettings to ConfigurationManager.AppSettings you would not get the same special benefits that you would get by using the special ConnectionStrings element.
Here are three benefits you will get:
- We can encrypt our connection strings on the fly using reg_iis.exe (or by other methods)
- We can add SQL Cache Dependencies
- By putting connection strings in the right place, we can take advantage of any new features that come out that use the ConnectionStrings element
The incorrect way of storing connection strings:
Dim objConn As New SqlConnection(ConfigurationManager.AppSettings("ConnectionString"))
Here is the new node we can add to Web.Config
<connectionStrings >
<add name="DefaultConnection" connectionString="server=(yourserver);uid=whatever;pwd=your_plaintext_stored_password;database=DEV123"
providerName="System.Data.SqlClient" />
</connectionStrings>
To use the updated connectionstring element, see below (C# example)
ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString
This is the preferred way to store and use your connectionstrings in .NET 2.0, allows you to do advanced stuff like encrypting your connection strings, using sqlParameterCache, etc.. If you want to encrypt your connection strings, the first step is putting them in the right place
ConfigurationManager is part of the Microsoft Application Blocks
Follow Up
-
How to encrypt connection strings using reg_iis.exe by Maurice Reeves (Google cache)
There are other benefits too that I did not document here.
Related Reading:
Other Interesting Posts
-
Articles
- January 2011
- April 2010
- March 2010
- February 2010
- January 2010
- August 2009
- July 2009
- June 2009
- May 2009
- April 2009
- February 2009
- December 2008
- November 2008
- October 2008
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008
- February 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- July 2007
- June 2007
- May 2007
-
Meta







