← Back to Database Operations with ASPPY
Lesson 2

Querying Data with Recordsets

Database Operations with ASPPY · Created 2026-06-01 13:34:28

Retrieve rows from the database using SELECT and iterate with a Recordset.

Basic SELECT
Dim rs
Set rs = conn.Execute('SELECT id, title FROM courses')
While Not rs.EOF
Response.Write rs('title') & '<br>'
rs.MoveNext
Wend
rs.Close : Set rs = Nothing

Recordset Properties
rs.EOF - True when no more rows
rs.Fields.Count - number of columns
rs('colname') or rs(0) - read a field
rs.MoveNext - advance to next row

Single Row
Set rs = conn.Execute('SELECT COUNT(*) FROM users')
Dim count : count = CInt(rs(0))
rs.Close

Empty Result Check
Set rs = conn.Execute(sql)
If rs.EOF Then Response.Write 'No rows found'

Tips
Always close recordsets before closing the connection
Use CInt/CDbl for numeric fields to avoid type issues
Recordset fields are 0-indexed if accessed by position

Live Demo →