|
Q5804 How do I connect to and display records from a database?
irt.org | Knowledge Base | ASP | Q5804 [ previous next ]
Q5804 How do I connect to and display records from a database?
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
%>
|
|
|
Copyright © 1996-2008 irt.org, All Rights Reserved.