[ Pobierz całość w formacie PDF ]
.Fields[0].AsInteger;finallyEmplCountData.Close;end;Font := TFont.Create;tryFont.Name :=  Courier New ;Font.Size := 9;PrintOutDataSet (EmplData, ProgressBar1, Font);finallyFont.Free;end;end;ClientDataSet and MyBaseThe general idea of a client/server application implies that the computation workload isshared between two separate programs, the RDBMS and a client application.Although it isvery hard to strike a precise line between the two sides, it is certainly useful to do operationson the client.Most database engines (BDE, as we ve seen in this chapter, and ADO, as we llsee in Chapter 16,  ActiveX Data Objects ) can manipulate client-side data stored in a cache.Using the ClientDataSet component, you can do the same regardless of the database engineyou are using, which makes your program more flexible, particularly if you want to usedbExpress, which doesn t provide a similar feature natively.A practical example will underline what I mean: Suppose you ve written a SQL query toretrieve a rather large dataset, and a user wants to see the same data in a different order.Youcan certainly run a new query, with the proper order by clause, but this implies sending thesame (possibly large) dataset once more from the server to the client.Since the client alreadyhas the data in memory, it would be more practical and generally faster to re-sort the data inmemory and present the same data to the user with a different ordering.The ClientDataSet component allows you to do this: Attaching the code to sort the databy assigning a proper field name to the IndexFieldNamesproperty.This is often accomplishedwhen the user clicks the field title in a DBGrid component (firing the OnTitleClickevent):procedure TForm1.DBGrid1TitleClick(Column: TColumn);beginClientDataSet1.IndexFieldNames := Column.Field.FieldName;end;Copyright ©2001 SYBEX, Inc., Alameda, CA www.sybex.com 2874c14.qxd 7/2/01 4:34 PM Page 642642 Chapter 14 " Client/Server ProgrammingTIPUnlike local databases, a ClientDataSet can have dynamic indexes, as they are computed inmemory anyway.The component also supports indexes based on a calculated field, specificallyan internally calculated field, a type of field available only for this dataset.Unlike ordinary cal-culated fields, which are computed every time the record is used, values of internally calcu-lated fields are kept in memory.This is why indexes consider them as plain fields.Indexing is not all the ClientDataSet has to offer.When you have an index, you can definegroups based on it, possibly with multiple levels of grouping.There is even specific supportfor determining the position of a record within a group (first, last, or middle position).Overgroups or entire tables, you can define aggregates; that is, you can compute the sum or aver-age value of a column for the entire table or the current group on-the-fly.The data doesn tneed to be posted to a physical server, because these aggregate operations take place in mem-ory.You can even define new aggregate fields, to which you can directly connect data-awarecontrols.I ll explore these capabilities in the next section.Another very interesting area of the ClientDataSet component is its ability to handle theupdates log, undoing changes, looking at their list before committing them, and so on.I llexplore this next.The ClientDataSet component supports many features, only some of which are related tothe three-tier architecture (covered in Chapter 17).This component represents a databasecompletely mapped in memory and can also be made persistent to a local file.Borland mar-keting has introduced the name MyBase to describe this feature of the ClientDataSet com-ponent, which was formerly called the briefcase model.The important thing to keep in mind is that all of these features are available to any client/server and even local applications.The ClientDataSet component, in fact, can get its datafrom a remote connection, from a local dataset (as you must do with dbExpress), or from alocal MyBase file.This is another huge area to explore, so I ll simply show you a couple ofexamples highlighting key features.WARNINGThe use of the ClientDataSet component, in each of its incarnations, requires either thedeployment of the Midas.dll library or the inclusion in the project of the MidasLib unit (avail-able in compiled format only).The core code of this component, in fact, is not directly part ofthe library and is not available in source code format.This is unfortunate, as many Delphi devel-opers are accustomed to debugging into the source code and using it as the ultimate refer-ence.It is noteworthy, though, the inclusion in Delphi 6 of the DCU version of the library, obtainedfrom a C-language source code.This allows you to avoid deploying the actual library along withyour program.Copyright ©2001 SYBEX, Inc., Alameda, CA www.sybex.com 2874c14.qxd 7/2/01 4:34 PM Page 643ClientDataSet and MyBase 643The Packets and the CacheThe ClientDataSet component reads data in packets made of the number of records indi-cated by the PacketRecords property.The default value of this property is  1, which meansthat the provider will pull all the records at once (this is reasonable only for a small dataset).Alternatively, you can set this value to zero to ask the server for only the field descriptors andno actual data or use any positive value to specify an actual number.If you retrieve only a partial dataset, as you browse past the end of local cache, if FetchOn-Demand property is set to True (the default value), the ClientDataSet component will getmore records from its source.This same property also controls whether BLOB fields andnested datasets of the current records are fetched automatically (these values might not bealready part of the data packet, depending on the value of the Options of the datasetprovider).If you turn off this property, you ll need to manually fetch more records, by calling theGetNextPacket method, until the method returns zero.(You ll call FetchBlobs and Fetch-Details for these other elements.)WARNINGNotice, by the way, that before you set a index for the data, you should retrieve the entiredataset (either by going to its last record or by setting the PacketRecords property to  1).Otherwise you ll have an odd index based on partial data.FilteringAs with any other dataset, you can use the Filter property to specify the inclusion in thedataset of portions of the data the component is bound to.When manipulating a large table,of course, you should use a proper query so that you don t retrieve a large dataset from aSQL server.Filtering up-front in the server should generally be your first choice.However, local filtering in the ClientDataSet can be quite useful, particularly becausethe filter expressions you can use are much more extensive than those you can use withother datasets.In particular, you can use the standard comparison and logical operators(Population > 1000 and Area 0 and x 1 dobegincds.Prior;Dec (i);end;// return to the current recordi := TMyGrid(DbGrid1).Row;cds [ Pobierz caÅ‚ość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • hanula1950.keep.pl