[ Pobierz caÅ‚ość w formacie PDF ] .The DataSource component checks this value before allowing an editing operation.A solution to this problem is to avoid the use of data-aware controls, as discussed in the lastchapter, and use specific SQL queries to update, insert, and delete records.A better approach is to automate this process (retaining the capabilities of the data-awarecontrols) by using the UpdateSQL component together with the Query component.TheUpdateSQL can be used only in conjunction with cached updates, a topic discussed in the lastchapter.The basic idea is that the update operations are kept in a local cache until the programcalls the ApplyUpdatesmethod of the Query component.This operation corresponds to the exe-cution of a series of update, insert, and deleteSQL operations on the server, using the data inthe cache.The required SQL commands are held by the UpdateSQL component, which has adesign-time editor you can use to generate these SQL commands almost automatically.Cached updates solve the live queries issue, reduce network traffic, define a standard wayto solve updates conflicts, and reduce the server load, but they require more memory on theclient computer.Copyright ©2001 SYBEX, Inc., Alameda, CA www.sybex.com2874c14.qxd 7/2/01 4:33 PM Page 613Client/Server with the BDE 613TIPA much better approach than using cached updates is to rely on the ClientDataSet compo-nent, which is extremely powerful and allows you to do similar things yet leaving you a lotmore programmatic control.The UpdateSQL ComponentThe role of the UpdateSQL component is to provide a query with the update statementsrequired to make its result set editable.Its key properties are DeleteSQL, InsertSQL, andModifySQL, but the most important element is the UpdateObject property of the relatedQuery component.The update SQL statements are executed when you apply the cachedupdates, sending the changes to the server.Because cached updates maintain the informationon the original records, the updates usually indicate which record to update by passing theoriginal data.This is the only way we have to identify a record on a SQL server, and thistechnique also helps the server to track any updates on the same record done by other users.All this setup might seem to imply a lot of work, but it is actually very simple.After you vewritten a query, you can connect the UpdateSQL component to it and activate the compo-nent editor, as shown in Figure 14.1.FI GURE 14.1:The UpdateSQL componenteditor in actionThis component editor has two tabs.The first indicates the criteria used to generate the SQLstatements for adding, deleting, or modifying records.With a join, you can select the table toupdate and the fields involved.When you ve completed this step, click the Generate SQLbutton and the editor will move to the second tab, where you can inspect the generated SQLcode for the three operations.Copyright ©2001 SYBEX, Inc., Alameda, CA www.sybex.com2874c14.qxd 7/2/01 4:33 PM Page 614614 Chapter 14 " Client/Server ProgrammingThe UpdateSQL ExampleTo demonstrate the real power of the UpdateSQL component, I ve built a complex examplecalled UpdateSQL, based on the Employee, Department, and Job tables of the IBLocal data-base we ve used in the past.Here is the textual description of the UpdateSQL component ofthe example:object EmpUpdate: TUpdateSQLModifySQL.Strings = ( update EMPLOYEE set FIRST_NAME = :FIRST_NAME, LAST_NAME = :LAST_NAME, SALARY = :SALARY, DEPT_NO = :DEPT_NO, JOB_CODE = :JOB_CODE, JOB_GRADE = :JOB_GRADE, JOB_COUNTRY = :JOB_COUNTRY where EMP_NO = :OLD_EMP_NO )InsertSQL.Strings = ( insert into EMPLOYEE (FIRST_NAME, LAST_NAME, SALARY, DEPT_NO, JOB_CODE, JOB_GRADE, JOB_COUNTRY) values (:FIRST_NAME, :LAST_NAME, :SALARY, :DEPT_NO, :JOB_CODE, :JOB_GRADE, :JOB_COUNTRY) )DeleteSQL.Strings = ( delete from EMPLOYEE where EMP_NO = :OLD_EMP_NO )endTo delete the employee records, the program uses a stored procedure, which is alreadyavailable in the sample database and is connected to the following component:object spDelEmployee: TStoredProcDatabaseName = AppDBStoredProcName = DELETE_EMPLOYEEParamData =endCopyright ©2001 SYBEX, Inc., Alameda, CA www.sybex.com2874c14.qxd 7/2/01 4:33 PM Page 615Client/Server with the BDE 615The OnUpdateRecord event of the Query component uses the stored procedure instead ofthe default UpdateSQL component for deleting records.Here is the code of the event handler:procedure TdmData
[ Pobierz całość w formacie PDF ] zanotowane.pldoc.pisz.plpdf.pisz.plmikr.xlx.pl
|