Home

ASP ADVANCED

In Chapter "ActiveX Data Objects," you learned how to use the ActiveX Data Objects (ADO) connection object to open a database connection.  You also learned how to use an open connection to execute SQL commands and return Recordsets.  However, the methods for manipulating the Recordset object were not discussed.

This chapter explains how to use the Recordset object.  In the first section, you learn the basic methods for displaying data with this object.  In the second section, you learn how to open a Recordset with different cursor and locking types.  Finally, the third section describes a number of advanced methods for working with the records in a Recordset.

A Recordset can be used to represent the records in a database table.  Like a table, a Recordset contains one or more records (rows).  Each record contains one or more fields (columns).  At any given moment, only one record is the current record.

To create a new instance of a Recordset object, you can use the Execute( ) method of the Connection object.  When you Use the Execute( ) method to return results from a database query, a new Recordset is created automatically.  Here's an example:

<%
Set MyConn=Server.CreateObject("ADODB.Connection")
MyConn.Open "FILEDSN=d:\Program Files\Common Files\ODBC\Data Sources\MyData.dsn"
Set RS=MyConn.Execute("SELECT * FROM MyTable")
RS.Close
MyConn.Close
%>

In this example, a SQL SELECT statement is used to retrieve all the records from a table named MyTable.  The Execute( ) method returns a Recordset.  In this script, the Recordset is assigned to the variable named RS.  The Recordset is then closed.  Finally, the connection is closed.

Each record in the RS Recordset corresponds to a record in the MyTable table.  To display all the records in the Recordset, you can simply loop through the records, as in the following example:

<%
Set MyConn=Server.CreateObject("ADODB.Connection")
MyConn.Open "FILEDSN=d:\Program Files\Common Files\ODBC\Data sources\MyData.dsn"
Set RS=MyConn.Execute("SELECT * FROM MyTable")
WHILE NOT RS.EOF
Response.write("<BR>"&RS("MyColumn"))
RS.MoveNext
WEND
RS.Close
MyConn.Close
%>

In this example, the WHILE...WEND loop is used to move through each record contained in the RS Recordset.  The MyColumn field of each record is outputted to the browser.  This script displays all the records contained in the MyTable table.

When a Recordset is first populated with records, the current record is always the first record.  In the preceding example, the MoveNext method of the Recordset object is used to move to the next record.  When all the records have been displayed, the EOF property of the Recordset object has the value True and the WHILE...WEND loop is exited.

A Recordset object has a Fields collection that contains one or more Field objects.  A Field object represents a particular column in a table.  For example, in the preceding script, the column MyColumn is displayed by using the expression RS("Mycolumn").  Actually, you can display the value of a column in a number of ways.  Each of the following expressions displays the value of the column named MyColumn:

RS("MyColumn")

RS(0)

RS.Fields("MyColumn")

RS.Fields(0)

RS.Fields.Item("MyColumn")

RS.Fields.Item(0)

Notice that you can refer to a particular field either by its name or by its ordinal number.  For example, you can refer to the MyColumn field either by using RS ("MyColumn") or by using RS (0). Either method works because the field named MyColumn corresponds to the first column in the table (the first field is the zero field).

Referring to a field by its ordinal number is useful when you don't know the names of the fields in a Recordset.  For example, the following Active Server Page script (continued on the next page) displays all the columns and all the rows in a table:

<HTML>
<HEAD><TITLE> Show All Rows And Columns </TITLE></HEAD>
<BODY>
<%
Set MyConn=Server.CreateObject("ADODB.Connection")
MyConn.Open "FILEDSN=d:\Program Files\Common Files\ODBC\Data Sources\MyData.dsn"
set RS=MyConn.Execute("SELECT * FROM MyTable")
%>
<TABLE BORDER=1>
<TR>
<% FOR i = 0 to RS.Fields.Count - 1 %>
<TH><% = RS(i).Name %></TH>
<% Next %>
</TR>
<% While Not RS.EOF %>
<TR>
<% FOR i = 0 TO RS.Fields.Count - 1 %>
<TD><% = RS(i) %></TD>
<% Next %>
</TR>
<%
RS.MoveNext
WEND
RS.Close
MyConn.Close
%>
</TABLE>
</BODY>

</HTML>

In this example, the Count property of the Fields collection is used to return a count of the number of fields in the Recordset.  The Name property is used to retrieve the name of each field.  Both of the FOR...NEXT loops are used to walk though all the fields in the Recordset.  No matter how many columns and rows the table contains, they'll all be displayed.