devxlogo

Quickly Establish a Database Connection from ASP Using File DSN

Quickly Establish a Database Connection from ASP Using File DSN

To quickly establish a database connection from ASP, try using a file DSN. The DSN string should not contain spaces, either, before or after the equal sign (=). In this case, the recordset object’s Open method refers to the file based DSN, containing location and configuration information about the database. Remember to create the file DSN before using it in the code. Another option is to refer directly to an explicit provider, data source, user ID, and password, rather than to a DSN.

An example to access a database from ASP using file DSN is shown below (remember to put the code in <% and %>):

  ' Create connection object Set cn = Server.CreateObject("ADODB.Connection") cn.Open "FILEDSN=TestWeb.dsn"   strSQL = "select FirstName,LastName FROM person"    'Instantiate a Recordset object  Set rsResults = Server.CreateObject("ADODB.Recordset")  rsResults.Open  strSQL, cn    Set objFirstName = rsResults("FirstName")   Set objLastName = rsResults("LastName")      Do Until rsResults.EOF      Response.Write objFirstName & " " & objLastName & "
" rsResults.MoveNext Loop ' Clean up is a good practice cn.Close Set rsResults = nothing Set cn = nothing
See also  Professionalism Starts in Your Inbox: Keys to Presenting Your Best Self in Email
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist