Features and Techniques > Using TData in Distributed Applications (disconnected recordset support) |
Building distributed applications, it is a common practice to use disconnected recordsets (Recordset.ActiveConnection = Nothing). A disconnected recordset is created on the server and marshaled (passed) to the client. When client updates must be saved in the database, the disconnected recordset is passed back to the server, re-connected to the database, and changes are saved in the database with Recordset.UpdateBatch. To use TData in a distributed application, you need to do the following:
Set TData.LockType = adLockBatchOptimistic and TData.ManualUpdate = True.
Create a disconnected recordset (with Recordset.ActiveConnection=Nothing and Recordset.LockType=adLockBatchOptimistic) and set TData.SourceRecordset to that recordset at run time.
When the end-user modifies data, the changes are stored in TData.Recordset, while TData.SourceRecordset remains unchanged, and update does not occur until explicitly triggered by programmer's code (for example, from a Submit command button). This is how TData.LockType = adLockBatchOptimistic works.
In a Submit button code, or other code updating user changes to the database, write the following:
Example Title |
Copy Code
|
---|---|
TData1.Recordset.UpdateBatch update_procedure(TData1.SourceRecordset) TData1.SourceRecordset.UpdateBatch |
Here update_procedure is a subroutine or an object method (it may be a business object) passing TData.SourceRecordset to the server for update. Write all the necessary code to update the database (on the server) in this procedure/method. If you already have pre-built classes for database update, use them here.
This is how it works: TData.Recordset.UpdateBatch sends users changes to TData.SourceRecordset (before this, TData.SourceRecordset remained unchanged), it does not save the changes in the database, because TData.ManualUpdate is set to True. Then modified SourceRecordset is sent to the server for update. After successful update, you need to reset the state of TData.SourceRecordset rows, to reflect the fact that there are no longer pending changes. This is done by TData.SourceRecordset.UpdateBatch. It does not perform any update (SourceRecordset is disconnected, so there is no database to update), it just resets the pending change flags. Depending on your application logic, you may consider refreshing TData with new data, fetched from the server, at this point, because data could change as a result of database update. But this is optional, necessary only if your application logic requires it.