ADO: Update(), AddNew(), GetRows() - Simplified Usage

ADO: Update(), AddNew(), GetRows() - Simplified Usage

Postby nageswaragunupudi » 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: Select all  Expand view

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: Select all  Expand view

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: Select all  Expand view

oRs:Update( aCols, aVals )
 


Similarly the familiar coding for AddNew() is:
Code: Select all  Expand view

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: Select all  Expand view

oRs:AddNew( { "FIRST", "LAST", "AGE", "SALARY" }, { cFirst, cLast, nAge, nSalary } )
 

OR
Code: Select all  Expand view

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: Select all  Expand view

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: Select all  Expand view

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
User avatar
nageswaragunupudi
 
Posts: 10632
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: Google [Bot] and 95 guests