Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Web
Expertise: Beginner
Aug 27, 1999



Building the Right Environment to Support AI, Machine Learning and Deep Learning

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 & "<BR>"

   ' Clean up is a good practice
  Set rsResults  = nothing
  Set cn = nothing
Jai Bardhan
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date