Home > Connection strings > Connection Strings in Asp.Net

Connection Strings in Asp.Net


 

           Please note that this article applies to the ASP.NET 2.0 framework and later. With the ASP.NET 2.0 configuration API, there is a new way to retrieve AppSettings and ConnectionString sections from web.config in the ASP.NET applications. New ConfigurationManager class allows you to access configuration files and replaces the ASP.NET 1.x’s ConfigurationSettings class, which is deprecated.

         You can use the ConfigurationManager API’s AppSettings and ConnectionStrings classes to access pre-defined web.config appSettings and connectionStrings sections respectively.

1. Sample Web.Config section with a Appsettings:

<script>
  <appSettings>
    <add key="XMLPath" value=~/testXML.xml/>
  </appSettings>
 <script>

 

 

Now lets get the xml path from the Web.Config file with only one line of code:


string xmlPath = ConfigurationManager.AppSettings ["XMLPath” ];

 

2. Sample Web.Config section with a ConnectionString:

<connectionStrings>
    <add name="ApplicationServices"
connectionString="Data Source=COMP-43;Ini Catalog=ram;Integrated Security=True"
         providerName="System.Data.SqlClient" />
  </connectionStrings>

 

Now lets get the ConnectionString from the Web.Config file with only one line of code:


string strConnString = ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString;

  1. No comments yet.
  1. No trackbacks yet.

Leave a comment