Writes dataset's pending cached updates to a database.
procedure ApplyUpdates; overload; virtual;
Call the ApplyUpdates method to write a dataset's pending cached updates to a database. This method passes cached data to the database, but the changes are not committed to the database if there is an active transaction. An application must explicitly call the database component's Commit method to commit the changes to the database if the write is successful, or call the database's Rollback method to undo the changes if there is an error.
Following a successful write to the database, and following a successful call to a connection's Commit method, an application should call the CommitUpdates method to clear the cached update buffer.
Note: The preferred method for updating datasets is to call a connection component's ApplyUpdates method rather than to call each individual dataset's ApplyUpdates method. The connection component's ApplyUpdates method takes care of committing and rolling back transactions and clearing the cache when the operation is successful.
The following procedure illustrates how to apply a dataset's cached updates to a database in response to a button click:
procedure ApplyButtonClick(Sender: TObject);
begin
with Query1 do begin
Connection1.StartTransaction;
try
// ... modify data
ApplyUpdates; // try to write the updates to the database
Connection1.Commit; // on success, commit the changes
except
RestoreUpdates; // restore update result for applied records
Connection1.Rollback; // on failure, undo the changes
raise; // raise the exception to prevent a call to CommitUpdates
end;
CommitUpdates; //on success, clear the cache
end;
end;