How to: Read Connection Strings from the Web.config File
This example reads a connection string from a Web.config file. The connectionStrings element is a ConnectionStringSettingsCollection collection of ConnectionStringSettings objects. Working with collection elements can be slightly more complicated than working with other configuration elements.
To update a configuration setting, use the Save or SaveAs method of the configuration object. For more information, see Using the Configuration Classes. For additional code examples, see the ConnectionStringsSection class and related classes.
This example uses the non-static method of obtaining configuration data, which allows you to pull configuration information from any application. If you are going to obtain configuration information from the application in which your code resides, use the static method, which processes faster. For more information, see the Working with Local and Remote Configuration Settings section in ASP.NET Configuration API Overview.
A Visual Studio project with source code is available to accompany this topic: Download.
Note: |
|---|
This example applies to a Web site named MyWebSiteRoot. To run the sample, you will have to either run it in a Web site by that name or replace the string supplied to the OpenWebConfiguration method with the name of your Web site. |
System.Configuration.Configuration rootWebConfig = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/MyWebSiteRoot"); System.Configuration.ConnectionStringSettings connString; if (rootWebConfig.ConnectionStrings.ConnectionStrings.Count > 0) { connString = rootWebConfig.ConnectionStrings.ConnectionStrings["NorthwindConnectionString"]; if (connString != null) Console.WriteLine("Northwind connection string = \"{0}\"", connString.ConnectionString); else Console.WriteLine("No Northwind connection string"); }
This example requires:
-
A connectionStrings element in the root Web.config file that contains a connection named NorthwindConnectionString. The element might look like the following:
<connectionStrings> <add name="NorthwindConnectionString" connectionString="Data Source=serverName;Initial Catalog=Northwind;Persist Security Info=True;User ID=userName;Password=password" providerName="System.Data.SqlClient" /> </connectionStrings>The connectionStrings element is a direct child of the <configuration> element and a peer of the system.web element.
Security Note:When storing sensitive information such as user names and passwords in a configuration file, you should encrypt the sensitive values using protected configuration. For more information, see How To: Secure Connection Strings when Using Data Source Controls.
The configuration file should be protected on the server by using Windows security settings to limit who can read the file. The connectionString element can be encrypted in order to protect it. For details, see Encrypting Configuration Information Using Protected Configuration.
- 12/21/2009
- Muthu Vijayan
- 1/8/2010
- Muthu Vijayan
-Tom
- 12/23/2009
- navyjax2
A quick-and-dirty workaround is to statically code your connectionString into your app. Maybe put it in your global.asax or something.
webster.net@live.com
- 7/15/2009
- T. Webster
using System.Web.Configuration;
string connectionString = WebConfigurationManager.ConnectionStrings["SportsStore"].ConnectionString;
In a web application, the System.Web.dll is referenced by default. This gives you access to the System.Web.Configuration.WebConfigurationManager listed above.
The ConfigurationManager mentioned in the previous comments is good for non-web applications, but unneccessary for web applications. I say unnecessary because in web projects you must manually add a reference to the System.Configuration.dll in order to access its ConfigurationManager. You can avoid this extra step in web projects because System.Web is automatically referenced in web projects - giving you immediate access to the WebConfigurationManager.
- 7/14/2009
- Mark Phillips
- 7/14/2009
- Mark Phillips
Dim connString As String =
ConfigurationManager.ConnectionStrings("NorthwindConnectionString").ConnectionString
The type for connString should be String as ConnectionString is a String value... And setting it this way is clearly easier than the above example.
- 5/21/2009
- eldoran
- 1/13/2009
- UncleShooter
Surely, it is much easier to do the following:
Dim connString As System.Configuration.ConnectionStringSettings =
ConfigurationManager.ConnectionStrings("NorthwindConnectionString").ConnectionString
- 11/14/2008
- A. Stirling
Note: