In Connecting to a Database, we have shown the essential steps to establish and open a connection to a data source from within a Web Forms page. In this application, we use a slightly different approach of storing the connection string in the application configuration file ( web.config ), thereby making the connection string globally available to any page in the application. This technique is basically implemented as follows.
In the web.config file for the application, include the following:
add key="myDbConn" value="server=servername; uid=userid; pwd=password; database=dbname">
changing the values ( shown in italics ) to correspond to your settings.
Then on each page that you need to initialize a connection, use:
SqlConnection myConn = new SqlConnection
( ConfigurationSettings.AppSettings [ "myDbConn" ] );
The Vagrant (The Vagrant #1), by Peter Newman
14 hours ago