RAD Studio
ContentsIndex
PreviousUpNext
Using the BDE to cache updates
Name 
Description 
To apply cached updates to one or more datasets in the context of a database connection, call the database component's ApplyUpdates method. The following code applies updates to the CustomersQuery dataset in response to a button click event:  
At design time, you can use the Update SQL editor to write the SQL statements for the DeleteSQL, InsertSQL, and ModifySQL properties. If you do not use the Update SQL editor, or if you want to modify the generated statements, you should keep in mind the following guidelines when writing statements to delete, insert, and modify records in the base table.
The DeleteSQL property should contain only an SQL statement with the DELETE command. The base table to be updated must be named in the FROM clause. So that the SQL statement only deletes the record in the... more 
To use the BDE for cached updates, the BDE-enabled dataset must indicate that it should cache updates. This is specified by setting the CachedUpdates property to True. When you enable cached updates, a copy of all records is cached in local memory. Users view and edit this local copy of data. Changes, insertions, and deletions are also cached in memory. They accumulate in memory until the application applies those changes to the database server. If changed records are successfully applied to the database, the record of those changes are freed in the cache.
The dataset caches all updates until... more 
Update SQL statements use a special form of parameter substitution that enables you to substitute old or new field values in record updates. When the Update SQL editor generates its statements, it determines which field values to use. When you write the update SQL, you specify the field values to use.
When the parameter name matches a column name in the table, the new value in the field in the cached update for the record is automatically used as the value for the parameter. When the parameter name matches a column name prefixed by the string "OLD_", then the old... more 
The recommended approach for caching updates is to use a client dataset (TBDEClientDataSet) or to connect the BDE-dataset to a client dataset using a dataset provider. The advantages of using a client dataset are discussed in Using a client dataset to cache updates.
For simple cases, however, you may choose to use the BDE to cache updates instead. BDE-enabled datasets and TDatabase components provide built-in properties, methods, and events for handling cached updates. Most of these correspond directly to the properties, methods, and events that you use with client datasets and dataset providers when using a client... more 
 
Applying updates is a two-phase process that should occur in the context of a database component's transaction so that your application can recover gracefully from errors. For information about transaction handling with database components, see Managing Transactions.
When applying updates under database transaction control, the following events take place:
  1. A database transaction starts.
  2. Cached updates are written to the database (phase 1). If you provide it, an OnUpdateRecord event is triggered once for each record written to the database. If an error occurs when a record is applied to the database, the OnUpdateError event is triggered if you provide... more 
When the BDE-enabled dataset represents a stored procedure or a query that is not "live", it is not possible to apply updates directly from the dataset. Such datasets may also cause a problem when you use a client dataset to cache updates. Whether you are using the BDE or a client dataset to cache updates, you can handle these problem datasets by using an update object. 
When a BDE-enabled dataset applies its cached updates, it iterates through the changes recorded in its cache, attempting to apply them to the corresponding records in the base table. As the update for each changed, deleted, or newly inserted record is about to be applied, the dataset component’s OnUpdateRecord event fires.
Providing a handler for the OnUpdateRecord event allows you to perform actions just before the current record’s update is actually applied. Such actions can include special data validation, updating other tables, special parameter substitution, or executing multiple update objects. A handler for the OnUpdateRecord event affords you greater... more 
You can apply updates for individual BDE-enabled datasets directly using the dataset's ApplyUpdates and CommitUpdates methods. Each of these methods encapsulate one phase of the update process:
  1. ApplyUpdates writes cached changes to a database (phase 1).
  2. CommitUpdates clears the internal cache when the database write is successful (phase 2).
The following code illustrates how you apply updates within a transaction for the CustomerQuery dataset:  
To update a record in an associated dataset, an update object uses one of three SQL statements. Each update object can only update a single table, so the object's update statements must each reference the same base table.
The three SQL statements delete, insert, and modify records cached for update. You must provide these statements as update object's DeleteSQL, InsertSQL, and ModifySQL properties. You can provide these values at design time or at runtime. For example, the following code specifies a value for the DeleteSQL property at runtime:  
The Borland Database Engine (BDE) specifically checks for user update conflicts and other conditions when attempting to apply updates, and reports any errors. The dataset component's OnUpdateError event enables you to catch and respond to errors. You should create a handler for this event if you use cached updates. If you do not, and an error occurs, the entire update operation fails.
Here is the skeleton code for an OnUpdateError event handler:  
Copyright(C) 2008 CodeGear(TM). All Rights Reserved.
What do you think about this topic? Send feedback!