Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org
#

Q5804 How do I connect to and display records from a database?

You are here: irt.org | FAQ | ASP | Q5804 [ previous next ]

The most common way to connect to a database is to use ActiveX Data Objects, which comes as part of the Windows MDAC or Microsoft Data Access Components install (see How do I install ASP on my Win 95/98 machine? for details).

Here is the code (place in an *.asp page with or without HTML already in it). This code example requires a DSN called myDSN setup through ODBC on the Control Panel.

<%

Dim objConn
Dim counter ' TO COUNT RETURNED RECORDS
counter = 0

' USING THE NATIVE ASP SERVER OBJECT, CALL IT'S CreateObject
METHOD TO MAKE
' objConn AN INSTANCE OF THE ADO CONNECTION OBJECT:

Set objConn = Server.CreateObject("ADODB.Connection")

'  OPENS A DATA SOURCE NAME (DSN) CALLED myDSN.
objConn.Open "DSN=myDSN"

' OPEN A RECORDSET BY EXECUTING SOME SQL ON A TABLE IN THE
OPEN CONNECTION
Set RS = objConn.Execute("SELECT * FROM currentDogs")

' NOW RETURN THESE RECORDS IN A SIMPLE FORMAT TO THE WEB
PAGE:

Do While Not RS.EOF
	response.write "<p>"
	For each fld in RS.Fields
		response.write fld.Name & " = " _
		& fld.Value &"<br>"
	Next
	RS.MoveNext
	response.write "</p>"
Loop

' CLOSE THE CONNECTION AND THE RECORDSET
RS.Close
objConn.Close

%>

©2018 Martin Webb