21.2. Reading Recordsets
Problem
You want to read the data from a recordset.
Solution
Use the RecordSet.getItemAt( )
method
to get a record at a particular index. Use
RecordSet.getColumnNames( )
to get an
array
of the column names.
Discussion
You can think of recordsets as being composed
of
rows and columns, like a grid. Each row represents a single record,
and each column represents a field within each record. All recordsets
have a getItemAt( )
method that returns a record
object at a given row index.
#include "NetServices.as" // Create a recordset and populate it usingaddItem( )
(see Recipe 21.1). rs = new RecordSet(["COL0", "COL1", "COL2"]); rs.addItem({COL0: "a", COL1: "b", COL2: "c"}); rs.addItem({COL0: "d", COL1: "e", COL2: "f"}); rs.addItem({COL0: "g", COL1: "h", COL2: "i"}); // Get a single record from the recordset usinggetItemAt( )
. record1 = rs.getItemAt(1); // Output the values from the record object. Displays: d e f trace(record1.COL0 + " " + record1.COL1 + " " + record1.COL2);
The getItemAt( )
method is all you need, as long
as you know the column names. However, if you do not already know the
column names (remember, recordsets are often retrieved from the
server), use the getColumnNames( )
method to get
an array of the recordset’s column names. You can
then use that array to loop through all the columns of a given
recordset.
// Get a record from the recordset. record1 = rs.getItemAt(1); // Retrieve the column names. columnNames = rs.getColumnNames( ); /* Loop through ...
Get Actionscript Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.