ASP.Net 2.0 web.config ConnectionStrings

by top54u.com 07 Jul, 2008

ASP.Net 2.0 provides a new feature to define the connection string in web.config file for the database connection with ASP.Net Web applications. ConnectionStrings section of web.config file is new in ASP.Net 2.0 that enables you to declare the connection string globally in a safe separate file. You can access the connection string declares in the web.config file anywhere in your ASP.Net web application project. You can declare as many connection strings in the web.config file as you need. For example if you have number of database sources of different type such as SQL Server database, Access Database ore MySQL database, you can declare different connection strings in the ConnectionStrings sub section of Configuration Section of the web.config file.

 

Syntax of using ASP.Net 2.0 Web.config ConnectionStrings

 

<configuration>

<connectionStrings><add name="CONNECTION-STRING-NAME" connectionString="PLACE YOUR CONNECTION STRING HERE" providerName="CONNECTION PROVIDER NAMESPACE"></add>

 

</connectionStrings>

<system.web> <!-- SOME OTHER SECTIONS UNDER system.web SECTION -->

</system.web>

</configuration>

 

In the above syntax you can see that connection string is added using <add></add> element in the connectionstrings section. <add> element supports 3 attributes:

name: This attribute is used to define the name for the database connection string.

 

connectionString: This attribute is used to declare the connection string for the database connection according to the connection provider for ASP.Net 2.0 database connectivity drivers and database source type. Each type of database supports different types of connectivity drivers and connection string formats.

 

providerName: providerName attribute is optional parameter. It is used to pass the provider name class of ASP.Net that will provide the connectivity and Data Access Functions and layers.

Comments

8/27/2008 8:55:37 AM

raj

When same thing can be done using AppSettings ie, declaring Connection strings and using them in App Settings, why did they come up with new section in the webconfig file.

raj us

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

10/11/2008 3:28:46 AM




related videos.....
recent posts.....
top54u ezines.....