Categories
Data access

Get the Database Connection String

In asp.net, database connection strings that have been saved in the web.config file can be retrieved using the WebConfigurationManager.

You can use the WebConfigurationManager in the Page Load event of each page, strore it in a variable, then use the variable whenever you need to talk to the database in that page. But instead of going to get it in every page just do so once when the application starts…

In your Global.asax file create a public variable in which to store the connection string:

Public Shared connString As String

In the Application_Start procedure set it like this:

Web.Configuration.WebConfigurationManager.ConnectionStrings("name of you connection string in your web.config").ConnectionString

To get and use the variable, as it is declared in Global.asax, prefix it with the class name:

Global_asax.connString

Leave a Reply

Your email address will not be published.