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:
procedure TForm1.ApplyButtonClick(Sender: TObject); begin // for local databases such as Paradox, dBASE, and FoxPro // set TransIsolation to DirtyRead if not (Database1.IsSQLBased) and not (Database1.TransIsolation = tiDirtyRead) then Database1.TransIsolation := tiDirtyRead; Database1.ApplyUpdates([CustomersQuery]); end;
void __fastcall TForm1::ApplyButtonClick(TObject *Sender) { // for local databases such as Paradox, dBASE, and FoxPro // set TransIsolation to DirtyRead if (!Database1->IsSQLBased && Database1->TransIsolation != tiDirtyRead) Database1->TransIsolation = tiDirtyRead; Database1->ApplyUpdates(&CustomersQuery,0); }
The above sequence writes cached updates to the database in the context of an automatically-generated transaction. If successful, it commits the transaction and then commits the cached updates. If unsuccessful, it rolls back the transaction and leaves the update cache unchanged. In this latter case, you should handle cached update errors through a dataset's OnUpdateError event. For more information about handling update errors, see Handling cached update errors.
The main advantage to calling a database component's ApplyUpdates method is that you can update any number of dataset components that are associated with the database. The parameter for the ApplyUpdates method for a database is an array of TDBDataSet. For example, the following code applies updates for two queries:
if not (Database1.IsSQLBased) and not (Database1.TransIsolation = tiDirtyRead) then Database1.TransIsolation := tiDirtyRead; Database1.ApplyUpdates([CustomerQuery, OrdersQuery]);
TDBDataSet* ds[] = {CustomerQuery, OrdersQuery};
if (!Database1->IsSQLBased && Database1->TransIsolation != tiDirtyRead)
Database1->TransIsolation = tiDirtyRead;
Database1->ApplyUpdates(ds,1);
Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
|
What do you think about this topic? Send feedback!
|