ClientDataSet implies ------ reprinted "Delphi from getting started"

xiaoxiao2021-04-07  351

ClientDataSet implies ------ Reprinted "Delphi from Getting Started" May be repeated with the previous notes with repetitive places where ClientDataSet components support a lot of features, some of which are related to the three-level structure, and can also be used in other environments . This component illustrates a database full image in memory, which makes dynamic operations, such as establishing an index, other data sets typically do not support this feature. For example, in order to classify query, we usually re-execute it. In order to index a local table, you need to define an index. Only ADO data set has some dynamic index functions as ClientDataSet. Indexes are not all the features provided by ClientDataSet. When we have an index, we can be based on its definition group, which may be multi-level packets. For a location recorded in the group (head, tail or intermediate position), even specifically supported. In groups or entire data tables, we can define a total; that is, you can dynamically calculate the sum or average of the entire table or the current group. Data does not need to be sent to physical servers because these total operations occur in memory. We can even define new total fields and can be connected directly to the data sensitive control. Note that all these features can not only be used with the MIDAS application, but also with the client / server, even partial thinning applications. In fact, the ClientDataSet component can connect from remote MIDAS, local data collection (establishing data snapshot), or local files (just like in the briefcase mode, but use only the entire table defined in the client data collection) A data is obtained. This is another area that needs to be studied, so it will demonstrate two examples to highlight key features. These examples are not based on MIDAS, but based on local forms. 1. A interesting feature that defines the data type of abstract data VCL database support is that when we use ClientDataSet based on local files, you can define an abstract data type. Simply place a ClientDataSet component on the form, activate the editor for the Fielddefs property, add two fields and select the ftadt value for their DataType properties. Now, move to the childdefs attribute, and define the subfield, the following is the field definition of the ADTDEMO example: Fielddefs = ​​DataType = ftadt size = 2 End> Here, just enter a name for the clientDataSet's filename property, right-click the component, and select Create Table Command; we are ready to compile and run the application (after connecting the data sensitive components it). The data will automatically read from the supplied files, and the change will be saved in the file when closing the program. If you use the DBGRID to view the result data collection, it allows us to expand or compress the subfields of the ADT field.

We can provide its compressed value by defining the onGetText event (there is a default value in Delphi4, but Delphi5 is not): procedure tform1.clientDataSet1NameGetText (Sender: tfield; var text: string; displaytext: boolean; begin text : = ClientDataSet1NameFirstName.Asstring '' ClientDataSet1NameLastname.Asstring; End; 2, Dynamic Index Once there is data on the ClientDataSet, the data is all in memory. When we start the component based on the local file (as in the ADTDemo paradigm), the entire file is loaded to the total memory. This is different from the loading data from the Paradox data table (the field that is only loaded) is different. The advantage of putting the entire table in memory is that we can quickly classify it. With the ClientDataSet component, we can achieve classification by assigning the field names corresponding to the indexfieldnames property. In ADTDEMO (and many programs), the index change will execute when clicking the title of the DBGRID control: procedure tform1.dbgrid1titleclick (Column: tcolumn); begin if color.field.FullName = 'name' Then ClientDataSet1.indexfieldNames: = 'name.lastname' else clientDataSet1.indexfieldNames: = column.field.FullName; end; Due to the ADT definition, the program uses the FullName property of the field (not the FieldName property). In fact, for subfields, the index should be based on Name.lastName instead of LastName. And the ADT field cannot be indexed by itself, so if you select it, the program uses the lastname subfield as an index. These indexes are not persistent; they are not saved in the file, but only in memory is applied to data. Tip: ClientDataSet can have indexes based on calculating fields, especially internal calculation fields, which can only be used for this data set. 3. Once a group defines an index once the clientDataSet is defined, the data can be packetized by the index. In fact, a group defined as a list of continuous records (according to index), the value of the fields that are indexed in the record will not change. For example, if there is a country-based index, all addresses with the country will be classified as a group. The CDSCalcs example has a ClientDataSet component, which also reads its data from the Country table of the DBDemos database. This operation can be executed when designing, using the ASSIGN LOCAL DATA command of the ClientDataSet Component Shortcut Menu.

In order to read data at runtime, get a Updated snapshot, you can add a DataSetProvider component to the form, connect three components as follows: Object Table: TTable Active = true databasename = 'dbdemos' TableName = 'country.db' end Object DataSetProvider1: TDataSetProvider DataSet = Table1 End Object ClientDataSet1: TclientDataSet Provider = 'DataSetProvider1' End Let's take a look at the definition of the group. This definition can specify a packet level as an index to obtain the index together define: object clientdataset1: tclientdataset indexdefs = ​​indexname = 'clientdtaset1index1' as have a group of Thereafter, we can display the grouping structure to the user in DBGRID. Simply handle the ONGETTEXT event for the group field (in the example), only when the record is a group's first record is displayed: Procedure TForm1.ClientDataSet1ContinentgetText (Sender: Tfield; var text: string; displaytext: boolean) Begin if gbfirst in clientDataSet1.getGroupState (1) TEXT: = sender.Asstring else text: = '; end; 4, defining a total of the other functional features of the CLIENTDATASET component is support for total support. The total is a calculated value based on multiple records, such as the entire data table or a set of records (using the group logic we just discussed), a sum value or average value. The total is sustainable; For example, when the support is entered in the shipping list entry, the sum of the ship will be automatically recalculated. Note :: Total is incrementally maintained, rather than whenever there is a value change to re-calculate all values. Total update uses ClientDataSet tracking Delta. For example, when the field changes, in order to update the SUM, the ClientDataSet reads the old value from the total, and adds a new value. It only needs two calculations, even in the total group. Therefore, the total update is instantaneous. There are two ways to define a total. We can use ClientDataSet's aggregates attribute, or you can use the Fields editor to define the integration field. In both cases, we define a total expression that assigns it a name and will connect it with an index and a packet level (unless you want to apply it to the entire data table).

转载请注明原文地址:https://www.9cbs.com/read-132404.html

New Post(0)