ADO Recordset
The Recordset
object in ADO is used to hold the results of a database query. It allows you to navigate, read, and manipulate data retrieved from the database.
Key Topics
Creating a Recordset
Example
<%
Dim conn, rs
Set conn = Server.CreateObject("ADODB.Connection")
conn.Open "Provider=SQLOLEDB;Data Source=localhost;Initial Catalog=MyDatabase;User ID=myUser;Password=myPassword;"
Set rs = Server.CreateObject("ADODB.Recordset")
rs.Open "SELECT * FROM Employees", conn
Do Until rs.EOF
Response.Write(rs("EmployeeName") & "<br>")
rs.MoveNext
Loop
rs.Close
conn.Close
Set rs = Nothing
Set conn = Nothing
%>
Explanation: This example retrieves all records from the Employees
table and iterates through them using the Recordset
object.
Navigating the Recordset
Example
<%
rs.MoveFirst ' Move to the first record
Response.Write("First Record: " & rs("EmployeeName") & "<br>")
rs.MoveLast ' Move to the last record
Response.Write("Last Record: " & rs("EmployeeName") & "<br>")
%>
Explanation: This example demonstrates navigating the recordset to access the first and last records.
Key Takeaways
- The
Recordset
object holds the results of a query. - Use methods like
MoveNext
andMoveFirst
to navigate through the data. - Always close the
Recordset
to release resources.