Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


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

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>"
   rsResults.MoveNext
  Loop

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

 

 

 

 

 


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

 

 

Sitemap