Use ASP Include Files for Database Connectivity

Use ASP Include Files for Database Connectivity

To avoid repeating the following lines of code in every ASP page that needs a database connection:

 {Dim connObjSet connObj = Server.CreateObject("ADODB.Connection")connObj.ConnectionString = Session("ConnectionString")connObj.Open}

You can use an include file, which contains the above lines (and the connection string). Say you do this in a file named databaseConn.asp, and put it in your /scripts directory. databaseConn.asp might look something like this:


Then, in every page where you need database connectivity, you just need to add this one line:


Whenever you need to refer to a database connection object, such as in theOpen method of the recordset object, simply use objConn, the name of the connection object in databaseConn.asp.


Share the Post: