RecordSet 3: Fields collection improved

<< Click to Display Table of Contents >>

Navigation:  Examples > RecordSet >

RecordSet 3: Fields collection improved

Previous pageReturn to chapter overviewNext page

The sample below uses the fields collection as well to get the data from a recordset.

It is optimized, compared to the previous one, because the field objects are stored in local variables, outside

of the DO WHILE loop.

The bold/italic lines are changed in comparison to sample 2

 

FUNCTION Start

 LOCAL oConn AS AdoConnection

 LOCAL uError        AS USUAL

 LOCAL cbErr AS CODEBLOCK

 LOCAL oRs AS AdoRecordSet

 LOCAL oFld1, oFld2 AS AdoField

 cbErr := ErrorBlock({|oErr|_Break(oErr)})

 BEGIN SEQUENCE

 oConn := OpenConnection()

 ? "Open a STATIC cursor with Pessimistic Locking and a table name"

 ? "Get data through Fields Collection on RecordSet"

 ? "Optimize by saving the FIELD objects in two local variables"

 

 oRs := AdoRecordSet{}

 oRs:Open("Employee",oConn, AdOpenStatic, AdLockpessimistic, AdCmdTable)

 ? "Number of rows in resultset: ",oRs:RecordCount , oRs:Source

 wait

 oFld1 := oRs:FIELDs:[Item,&#32;"Emp_Id"]

 oFld2 := oRs:FIELDs:[Item,&#32;"LName"]

 DO WHILE .NOT. oRs:EOF .and. oRs:AbsolutePosition < 20

 // show 'record number' and column values

 // using the Fields Collection

 ? oRs:AbsolutePosition,OFld1:Value, oFld2:value

 oRs:MoveNext()

 ENDDO

 oRs:close()

 oConn:Close()

 

 

 RECOVER USING uError

 ? uError:description

 END

 wait

 ErrorBlock(cbErr)

 

 RETURN