Ado Recordset in ASP
ADO Recordset in Classic ASP is a key object used to hold the results of a query — basically, it represents a set of records (rows) returned from a database.
What is an ADO Recordset?
A container for rows and columns returned by a SQL query.
Allows navigation through rows (
MoveNext,MoveFirst, etc.).Lets you read or update data in the recordset.
Has properties like
.EOF(end of file),.BOF(beginning of file),.Fields,.RecordCount.
Basic Example: Using ADO Recordset in ASP
<%Dim conn, rs, sql' Create and open connectionSet conn = Server.CreateObject("ADODB.Connection")conn.Open "Provider=SQLOLEDB;Data Source=SERVER;Initial Catalog=DB;User ID=USER;Password=PASS;"' SQL querysql = "SELECT UserID, Name, Email FROM Users"' Create Recordset object and open itSet rs = Server.CreateObject("ADODB.Recordset")rs.Open sql, conn, 1, 3 ' 1=adOpenKeyset, 3=adLockOptimistic' Check if there are recordsIf Not rs.EOF Then Do Until rs.EOF Response.Write "UserID: " & rs.Fields("UserID") & "<br>" Response.Write "Name: " & rs.Fields("Name") & "<br>" Response.Write "Email: " & rs.Fields("Email") & "<hr>" rs.MoveNext LoopElse Response.Write "No records found."End If' Close recordset and connectionrs.CloseSet rs = Nothingconn.CloseSet conn = Nothing%>Important Recordset Properties
| Property | Meaning |
|---|---|
.EOF | True if after last record |
.BOF | True if before first record |
.RecordCount | Number of records in the recordset (may be -1 if unsupported) |
.Fields | Collection of fields (columns) in current record |
Important Recordset Methods
| Method | Purpose |
|---|---|
.Open | Open recordset with SQL and connection |
.Close | Close the recordset |
.MoveNext | Move to next record |
.MovePrevious | Move to previous record |
.MoveFirst | Move to first record |
.MoveLast | Move to last record |
Notes
Use cursor and lock types to control behavior (e.g.,
adOpenForwardOnly,adLockReadOnly).You can update data in a recordset if opened with a writable lock.
Always close and clean up objects to free resources.
Want me to show you how to add/update/delete records with Recordset, or how to use different cursor types?