FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour ADO: Update(), AddNew(), GetRows() - Simplified Usage
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
ADO: Update(), AddNew(), GetRows() - Simplified Usage
Posted: Wed Jul 31, 2013 12:17 AM
We all know the methods Update(), AddNew() and GetRows() of ADODB RecordSet. I propse here to consider a simpler alternative usage of these methods.

For example, let us consider oRs:Update() method. We are familiar with this way of usage:
Code (fw): Select all Collapse
oRs:Fields( "First" ):Value := cFirst
oRs:Fields( "Age" ):Value := nAge
// < for all fields >
// and then
oRs:Update()

Depending on the number of fields, this code may run into several lines and again in each line the laborious coding of "oRs:Fields( "<name>" ):Value := <newvalue>"
Instead, I suggest considering the following single line approach:
Code (fw): Select all Collapse
oRs:Update( { "FIRST", "LAST", "AGE", "SALARY" }, { cFirst, cLast, nAge, nSalary } )

In case we have the names of the fields and values ( read and edited ) in arrays like aCols and aVals, we can write
Code (fw): Select all Collapse
oRs:Update( aCols, aVals )


Similarly the familiar coding for AddNew() is:
Code (fw): Select all Collapse
oRs:AddNew()
oRs:Fields( "First" ):Value := cFirst
oRs:Fields( "Age" ):Value := nAge
// < for all fields >
// and then
oRs:Update()


Even here we have short-cut:
Code (fw): Select all Collapse
oRs:AddNew( { "FIRST", "LAST", "AGE", "SALARY" }, { cFirst, cLast, nAge, nSalary } )

OR
Code (fw): Select all Collapse
oRs:AddNew( aCols, aVals )

Important: When we use array of field names and values, we need not call oRs:Update()


Reading the present values of all the fields into an array;
Familiar method:
Code (fw): Select all Collapse
aVals := Array( oRs:Fields:Count() )
for i := 1 to oRs:Fields:Count()
   aVals[ i ] := oRs:Fields( i - 1 ):Value
next


We an make this process simpler and faster by using GetRows() method.
Code (fw): Select all Collapse
aVals := oRs:GetRows( 0, 1 )[ 1 ] // from current position (0) read one row (1)
aOriginals := AClone( aVals )  // later compare to check modified or not
oRs:MovePrevious()  // GetRows() advances the record pointer. Put it back


For sample usage of this approach, please see \fwh\samples\adoxbr01.prg
Regards



G. N. Rao.

Hyderabad, India

Continue the discussion