How Do You Use Global Connection String In Asp.net?

1

1 Answers

Naureen Khan Profile
Naureen Khan answered
In ASP.NET the best place to have your connection string is to in the web.config file. If you place it in the web.config file at the root of your web application, it can be accessed by all your web pages.

The reason(s) for placing it in the config file are:
1. You can have multiple connection strings with different names
2. If you need to change the connection after your application has been launched, simply make the change in the config file and no re-compilation of the website is required.

HOW TO DEFINE CONNECTION IN WEB.CONFIG FILE

To define a connection string, go to the CONFIGURATION section in WEB.CONFIG file.

Under CONFIGURATION section you can define your connection as APPSETTINGS or CONNECTIONSTRINGS

Example:












HOW TO RETRIEVE CONNECTIONS IN CODE

The connection strings defined in WEB.CONFIG can be used in your web application as follows:

To retrieve AppSettings

Dim s As String = System.Configuration.ConfigurationManager.AppSettings("MyDB1")

To retrieve ConnectionStrings

Dim t As String = System.Configuration.ConfigurationManager.ConnectionStrings("SQLDB ").ToString()

Answer Question

Anonymous