GetRows Method

See Also    Example    Applies To

Retrieves multiple rows from a CdbRecordset object.

Syntax

COleVariantGetRows(LONG lRows = -1);

Parameters

Type Example Description
LONG lRows The number of rows to retrieve. The default is all rows.

Remarks

Use the GetRows method to copy records from a CdbRecordset. GetRows returns a two-dimensional array. The first subscript identifies the field and the second identifies the row number. For example, intField represents the field and intRecord identifies the row number:

avarRecords(intField, intRecord)

To get the first field value in the second row returned, use code like the following:

field1 = avarRecords(0,1)

To get the second field value in the first row, use code like the following:

field2 = avarRecords(1,0)

The avarRecords variable automatically becomes a two-dimensional array when GetRows returns data.

The number of rows that you can retrieve is constrained by the amount of available memory. You shouldn't use GetRows to retrieve an entire table into an array if it is large.

Because GetRows returns all fields of the CdbRecordset into the array, including Memo and Long Binary fields, you might want to use a query that restricts the fields returned.

After you call GetRows, the current record is positioned at the next unread row. That is, GetRows has the same effect on the current record as Move lRows.

If you are trying to retrieve all the rows by using multiple GetRows calls, use the EOF property to be sure that you're at the end of the CdbRecordset. GetRows returns less than the number requested if it's at the end of the CdbRecordset, or if it can't retrieve a row in the range requested. For example, if you're trying to retrieve 10 records, but you can't retrieve the fifth record, GetRows returns four records and makes the fifth record the current record. This will not generate a run-time error. This might occur if another user deletes a record in a dynaset-type CdbRecordset. See the example for a demonstration of how to handle this.

Usage

#include <afxole.h>
#include <dbdao.h>

CdbRecordset      rst;
COleVariant      vRows, vVal;
LONG            lIndex[2], lMax = 10;
HRESULT         hr;

...                           // Open a recordset, etc.
vRows = rst.GetRows(lMax);   // Get 10 rows from the recordset.
lIndex[0] = 0;               // Index the first field.
                           // Index rows 0 through 9.
for (lIndex[1] = 0; lIndex[1] < lMax; lIndex[1]++)
{                           // Get the field.
hr = SafeArrayGetElement(vRows.parray, &lIndex, &vVal);
...                           // Process the field in vVal.
}