Update and Maintain dbExpress's Unidirectional, Read-Only Datasets (cont'd)
SQLClientDataSet: The Three-in-One Solution
Apart from using a TSQLDataSet that uses a TDataSetProvider to put the records in a TClientDataSet component, we could also have used a TSQLClientDataSet component that in effect contains the three aforementioned components in one. This means that we should also respond to the OnAfterPost and OnAfterDelete event handlers, only this time we must make sure to cast the DataSet argument to a TSQLClientDataSet (which unfortunately is not derived from a TClientDataSet).
procedure TForm1.SQLClientDataSet1AfterPost(DataSet: TDataSet);
begin
(DataSet AS TSQLClientDataSet).ApplyUpdates(-1)
end;
It feels a bit unsatisfying that we do not have a uniform (single-source) solution for both the TClientDataSet and TSQLClientDataSet eventsyet.
A Uniform and Reusable Solution
Although TSQLClientDataSet isn't derived from TClientDataSet (so we can't just use the "Sender AS TClientDataSet" for a TSQLClientDataSet), both are in fact derived from TcustomClientDataSet. So we should just change our code to be uniform and reusable:
procedure TForm1.SQLClientDataSet1AfterPost(DataSet: TDataSet);
begin
(DataSet AS TCustomClientDataSet).ApplyUpdates(-1)
end;
This should be connected to the OnAfterPost as well as the OnAfterDelete event handlers. It can operate on both the regular TClientDataSet and the TSQLClientDataSet components.
And of course, in both cases you still should respond to the OnDestroy or OnClose event handler to make sure the final change is taken into account as wellbefore you close your application.
Bob Swart (aka Dr.Bob) is a Delphi trainer and consultant who has spoken at the Borland Developer Conferences since 1993. As a freelance technical author, he has written chapters for The Revolutionary Guide to Delphi 2 (WROX), Delphi 4 Unleashed, C++ Builder 4 Unleashed, C++ Builder 5 Developer's Guide, and the upcoming Kylix Developer's Guide and Delphi 6 Developer's Guide (SAMS). He can be reached at drbob@chello.nl.
Do you expect dbExpress to replace the Borland Database Engine, especially since Kylix supports only dbExpress? Let us know in the Database Development discussion groups!