7 |
The LA Fox Developer Newsletter
ADO
Jumpstart
(Con’t from page 6)
?.Number,.Description
EndWith
Next oError
Endif
On Error &cOldErr
If you are thinking, “Hey, maybe I should write a wrapper class
to
better
encapsulate and centralize code,” you’re on the right
track. The following code creates a custom class that can serve
as a starting point:
Local oRecordsetHandler
oRecordsetHandler
=
CreateObject("RecordsetHandler")
oRecordset.FieldS(”COmpaflyflame”).VaIUe
=
“Alfreds Futterkiste”
If loRecordsetHandler.Update(OReCOrdSet)
oRecordsetHandler.canCel(OReCOrdSet)
Endif
Define Class RecordsetHandler As Custom
Protected oRecordset
Protected ErrFlag
Procedure Update(oRecordset)
This.oRecordset
=
oRecordset
oRecordset.UpdateBatch
Return !This.ErrFlag
EndProc
Procedure Cancel(oRecordset)
This.oRecordset
=
oRecordset
oRecordset.Cancel
Return !ThIs.ErrFlag
EndProc
Procedure Error(nError, cMethod, nLine)
Local oError
For Each oError In;
This.oRecordset.ActlveConnectiOfl.ErrOrS
With oError
?.Number,.Description
End
With
Next oError
Thls.ErrFlag
=
.T.
EndProc
EndDeflne
There’s a better way to determine whether an update proceeded
successfully. The preferred approach is to trap events that ADO
fires. Visual FoxPro by itself does not surface COM Events.
Fortunately, the new VFPCOM. DLL component provides this
capability to Visual FoxPro. The previous example
can
be
modified to show how using COM Events makes for more robust
code and class design.
Now we can improve the code of our example. Most of the time,
for efficiency, you will want to batch your updates that comprise
multiple records. Often, when you update multiple records,
transaction processing is required. In other words, either
updates to all records must succeed or none should occur. To
illustrate, let’s say you must apply a 10 percent price increase
to the products you sell. The prime requirement is that all
records in the Products table need modification. Without
transactional capabilities, the possibility exists that, for ex-
ample, after the first 10 records are updated, an
error
generated
April 2000
on the eleventh record prevents a complete update. Transaction
processing provides the ability to rollback changes.
The following example incorporates
error
trapping and the three
transaction methods of the Connection object:
Local
Err,cOldErr
cOldErr
=
On(”error”)
On Error
Err
=
.T.
oRecordset.Activeconnection.Begl nTra ns
Do While !oRecordset.Eof
If Err
Exit
Else
With oRecordset
.Fields(”unitprlce”).Value
=;
.Fields(”unitprice”).Value
*
1.1
.Movenext
EndWith
Endif
EndDo
oRecordSet.UpdateBatch
If Err
oRecordset.ActiveConnection.RollBackTrans
oRecordset.CancelBatch
Else
oRecordset.ActiveConnectlon.Com mitTrans
Endif
On Error &cOldErr
Additional operations you are likely to employ with recordsets
deal with adding new records and deleting existing records.
Both of these processes are very simple. The following code
adds a new record:
oRecordset.AddNew
As in Visual FoxPro, in ADO the new record becomes current.
Once the Add New method is invoked, the field can be populated
and, depending on the LockType, you then invoke either the
Update or UpdateBatch methods to modify the data source.
Deleting records is just as easy. The following code deletes the
current record:
oRecordset.Detete
Once again, after deleting the record, a call to Update or
UpdateBatch will update the data source.
(To
be continued...]
Join
LA Fox
Page
7
|
7 |