Displaying all Records from a Database in ASP
Download the code
The code below will simply display all the records from our "tblFriends"
table in the database called 'Friends.mdb'. The table has 3 columns,
firstly an 'ID' field that is an autonumber, secondly a field
called 'FirstName' which is a textfield and lastly another textfield
called 'SurName'.
Call our file 'display_records.asp'
<%@ Language="VBScript" %>
<% Option Explicit %>
<html>
<head>
<title>Displaying all records from a database table</title>
</head>
<body>
<%
Dim Connection, Recordset
Dim sSQL, sConnString
sSQL="SELECT * FROM tblFriends"
sConnString="PROVIDER=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & Server.MapPath("Friends.mdb")
Set connection = Server.CreateObject("ADODB.Connection")
Set recordset = Server.CreateObject("ADODB.Recordset")
connection.Open sConnString
recordset.Open sSQL,connection
If Recordset.EOF Then
Response.Write("No records returned.")
Else
Do While Not recordset.EOF
Response.Write recordset("ID") & " " &
recordset("FirstName") & " " & _
recordset("SurName")
Response.Write "<br>"
Recordset.MoveNext
Loop
End If
Recordset.Close
Connection.Close
Set Recordset = Nothing
Set Connection = Nothing
%>
</body>
</html>
The above code will display the ID number, the FirstName and SurName
of all the entries in your database table. If there are no entries then
the message 'No records returned.' will be displayed.
Site developed by Michael Wall - Web Design Belfast N.Ireland.
Copyright © 2000-2008. All rights reserved.
|