Multi-layer database development 11: TclientDataSet Select Blog from Cyrtsoft

xiaoxiao2021-03-06  60

Chapter 11 TClientDataSet is the same as TTable, TQuery, and TclientDataSet is also inherited from TDataSet, which is usually used for clients of multi-layer architectures. TclientDataSet's biggest feature is that it does not rely on BDE (Borland Database Engine), but it requires a dynamic link library support, this dynamic link library called dbclient.dll. On the client, you don't need to use the TDATABASE component because the client is not directly connected to the database. Since TclientDataSet is inherited from TDataSet, it supports functions such as editing, search, browsing, error correction, filtering. Since the TclientDataSet establishes the local copy of the data in memory, the above-described operation is very fast. It is also because TclientDataSet does not directly connect to the database, so the client must provide a mechanism for obtaining data. In Delphi 4, TclientDataSet has three ways to get data from the file. Get data from another data set from the local. Get data from the remote database server via the iProvider interface. In a client program, you can use the above three mechanisms to obtain data. 11.1 Browse and edit data and other data set components, you can display the data sets introduced by TclientDataSet by standard data controls, of course, this requires the TDataSource component. Since TClientDataSet is inherited from TDataSet, the TclientDataSet components are also roughly available in the functions supported from TDataSet. Different, TclientDataSet can establish a copy of the data in memory, so TclientDataSet adds some special functions than other data set components. 11.1.1 Browsing Data You can display the data set introduced by TclientDataSet with a standard data control. In the runtime, functions such as First, GotoKey, Last, Next, and Prior can be called to browse the data. TclientDataSet also supports bookmarking features, you can use bookmarks to mark a record, and you can easily find this record. For data set components such as TTABLE, TQuery, only the recno attribute can only determine the number of the current record. For the TclientDataSet component, you can also write the RecNo property, making the record of a number of serials becomes the current record. 11.1.2 CANMODIFY Attribute TDataSet's CANMODIFY property is used to determine whether the data in the data set can be modified. The CANMODIFY property itself is read-only, that is, the data can be modified does not depend on the application. However, the TclientDataSet component has its own specialty because TclientDataSet has created a copy of the data in memory, so the application can decide whether to modify the data. If the user is not allowed to modify the data, simply set the ReadOnly property to True. At this point, the CANMODIFY attribute will definitely return false. Unlike other data set components, modify the ReadOnly property of the TclientDataSet component, do not need to set the Active property to True in advance. 11.1.3 Cancel the basic unit of TclientDataSet transfer data is called a packet, and the current packet can be accessed by the DATA property. However, the user's modification of the data is not directly reflected in the Data property, but is temporarily written to a Delta property, which is the advantage of being able to cancel the modification at any time.

However, here will explain that although users' modifications are not reflected to Data, it is the latest modified data in the data control in the data control. If a record has been repeatedly modified many times, the user sees only the latest data, but the log is recorded many times. To cancel the last modification, call the undolastChange function. UndolastChange needs to pass a Boolean type parameter called FollowChange, if the FOLLOWCHANGE parameter is set to true, the cursor is moved to the recovered record, and if the FOLLOWCHANGE parameter is set to false, the cursor is still on the current record. ChangeCount Attributes Returns the number of modifications recorded in the log. If a record is repeatedly modified multiple times, each calling of the undolastchange will cancel the last modification step by step. UndolastChange can only cancel the last changes, if you want to cancel all modifications, first select a record, then call RevertRecord. RevertRecord will cancel all changes to the current record from the log. TclientDataSet also has a SavePoint property that saves the current editing state and can return the status at the time later. For example, you can save the current status: BeforeChanges: = ClientDataSet1.savePoint; later, you can restore the status of the status: ClientDataSet1.savePoint: = BeforeChanges; the application can save multiple states, you can restore one of the state, however, once A state is restored, and the status after it is invalid. If you want to cancel all modifications recorded in the log, you can call the CanceElupdates function. Cancelupdates will clear the log and cancel all modifications. If the Logchanges property is set to false, the user's modification of the user is directly reflected in the DATA property. 11.1.4 Merging Modification To combine the modifications recorded in the log into the DATA properties, there are two ways, which way specifically, depends on the mechanism of the application acquisition data. However, no matter which mechanism, the log is automatically emptied. For a program that gets data from a file, simply call the MergeChangeLog function, incorporate the modifications recorded in the log into the DATA property. Don't worry that other users have modified data at the same time. For a program that acquires data from the application server, you cannot call MergeChangeLog to merge data, and call the ApplyUpdates function, Applyupdates will pass the modifications recorded in the log to the application server, and the application server has updated the data to the database server. Will be merged into the DATA property. 11.1.5 Correction TclientDataSet supports error correction. In general, it is necessary to establish an error correction rule to correct the data input by the user. Also, if you get an iProvider interface, you can also introduce the error correction rule from the remote server. Sometimes, the client may need to temporarily prohibit error correction, because the client retrieved data from the application server is phased, and some error correction rules are likely to report in all data retries. To temporarily prohibit error correction, you can call DisableConstraints. To re-enable error correction, you can call the EnableConstraints function. DisableConstraints and EnableConstRAINTS are actually a count for an internal.

11.2 The index of the index has such a few benefits:. Location records in the data set are relatively fast. . You can create a Lookup or Master / Detail relationship between two datasets. You can sort records. In the multilayer architecture, when the client retrieves data from the application server, it has obtained the default index. The default index is called Default_order, you can use this index, but you cannot modify or delete this index. In addition to the default index, TClientDataSet also automatically creates a sub-index called ChangeIndex on the records recorded in the log. Like Default_order, this sub-index cannot be modified or deleted. In addition, you can use other indexes established in the data set, or build an index yourself. 11.2.1 Creating a new index To create a new index, you can call AddIndex. AddIndex needs to pass several parameters: First, the Name parameter is used to specify an index name. The name of the index is required when the index is running switching index. The second is the fields parameter, which is a string that specifies the field name in the index, separated from each other. The third is the options for setting the index, including the ixdescending element indicates in descending order, including the IXCaseInSensitive element indicates that case is not sensitive. The fourth is the descfields parameter, which is also a string that specifies several field names, which will be arranged in descending order. 5 is the caseinsfields parameter, its role is similar to the Descfields parameter, which is included in the field in the CaseinsFields parameter. Sixth is the GroupingLevel parameter, used to specify the packet level, its value cannot exceed the number of fields in the index. The following code creates an index: if edit1.text <> '' and clientdatatet1.fields.FindField (Edit1.text) ThenbeginClientDataSet1.AddIndex (Edit1.Text 'Index', Edit1.Text, [ixcaseinsensitive], ',' ', 0); ClientDataSet1.indexName: = Edit1.Text ' Index '; END; To avoid creating an index, you can temporarily use the indexfieldNames property to specify several fields to let the data sets sort these fields. 11.2.2 Deleting and Switching Index To delete a previously created index, you can call DeleteIndex and specify the index name to be deleted. Note: DEFAULT_ORDER and CHANGEINDEX cannot be deleted. If multiple indexes have been established, you can arbitrarily select one of the indexes, which is used to use indexName properties. 11.2.3 After selecting an index with an index, the data set will automatically be sorted by the fields. Thus, near records are often containing the same value on the key field. For example, suppose is that there is such: Salesrep Customer ORDERNO AMOUNT1 1 5 1001 1 2 501 2 3 2001 2 6 752 1 1 102 3 4 200 It can be seen that the value of the Salesrep field is repeated. For the value of the Salesrep field, the value of the Customer field is also repeated.

That is to say, you can packet with the salesrep field, then press the Customer field group. Obviously, the packet level here is different, and the packet established by the Salesrep field belongs to the first level, and the packet established by the Customer field belongs to the second level. In fact, the packet level depends on the order in the field in the index. The TclientDataSet can determine if the recorded value is displayed according to the packet level. For example, maybe it may be displayed in the following form: Salesrep Customer ORDERNO AMOUNT1 1 5 100 2 50 2 3 200 6 752 1 102 3 4 200 To determine where the current record is where the GetGroupState function can be called. The getGroupState function needs to pass a parameter for specifying the packet level. 11.3 The calculation field is the same as other datasets, or the calculated field can also be added in the data set established by TclientDataSet. The value of the calculating field is calculated based on other fields in the same record. In other data sets, as long as the user modifies the data or the current record changes, the oncalcfields event will be triggered, in other words, the value of the calculated field is calculated once. TclientDataSet introduces the concept of "internal calculation field". Unlike the general calculation field, the value of the internal calculation field will be accessed with the value of other fields, so that only the ONCALCFIELDS event is triggered if the user modifies the data. If only the current record is changed, the oncalcfields event will not trigger the oncalcfields event. . That is, the value of the internal calculation field needs to be re-calculated. In the handle of the oncalcfields event, first determine the state attribute. If the State property returns DSInternalcalc, the value of the internal calculation field is required at this time. If the State property returns DSCalcFields, the value of the general calculation field is required at this time. 11.4 Standard Value TclientDataSet Adds statistics, which can automatically calculate sum, average, count, maximum, and minimum based on packets. These statistics will automatically follow up when users edit data. 11.4.1 Specify how to specify how to specify how to specify, and use the aggregates properties. This property is a taggregates object that is used to manage a set of TaggRegate objects. At the design period, you can open the editor shown in Figure 11.1 on the edge of the Aggregates property. Figure 11.1 Managing a set of taggregate objects Click the button to add a taggRegate object, click the button to delete a taggregate object, click the button to move the taggregate object forward, click the button to move the taggregate object. You can create a field for expressing a statistical value with a field editor, which must be "aggregate". Delphi 4 automatically creates a TaggRegate object and is added to the aggregates properties. Select a TaggRegate object, the Object INPector will display the properties of the object. The expression attribute is used to specify statistical expressions, such as: SUM (Field1) can also be a complicated expression: SUM (Qty * Price) - SUM (AmountPAID) In ​​the expression, you can use the following status operators:. SUM calculates the sum of a set of data. .Avg calculates the average of a set of data. .Count calculates the number of non-null values ​​in a set of data. .Min calculates the minimum value of a set of data. .Max calculates the maximum value of a set of data.

In addition to the above-mentioned statistical operators, the operators that can be used in filters can be used, but they cannot nested. In an expression, there can be several statistical values ​​or constants, but statistics and fields cannot be mixed. SUM (Qty * Price) {legal} Max (Field1) - max (Field2) {legal} AVG (discountrate) * 100 {legal} MIN (SUM (field1)) {illegal, not nested} count (field1) - Field2 {Illegal, statistical values, and fields cannot be mixed in an expression} 11.4.2 Specify group By default, statistics are calculated based on all records in the data set. However, the statistical value can also be calculated for a part of the record, which requires a packet in advance. The concept of packets has been mentioned in front of the index. You can choose which index and the largest packet level can be selected via the indexName property and the GroupingLevel property. For example, suppose there is a table: Salesrep Customer ORDERNO AMOUNT1 1 5 1001 1 2 501 2 3 2001 2 6 752 1 1 102 3 4 200 If you are packet in the SalesRep field, and specify the first level, the program code should Write this: agg.expression: = 'SUM (Amount)'; agg.indexname: = 'Salescust'; agg.groupinglevel: = 1; agg.aggregatename: = 'Total for rep'; 11.4.3 How to get statistics Get a statistical value, you can call the value function of the TaggRegate object. If the statistical value is calculated based on all records in the data set, you can call the Value function at any time. If the statistical value is based on the packet, it must be guaranteed that the current record is located within the group. Therefore, before calling value, it is best to call the getGroupState function to see if the current record is within the group. To display a statistical value in the data control, you must create a permanent field object in advance in the field editor, which must be aggregate. 11.5 Data Pack The data that can access the client from the application server via the DATA property. The program is now as follows: Procedure TFORM1.BUTTON1CLICK (Sender: TOBJECT); beginclientDataSet1.data: = clientDataSet1.Provider: = clientDataSet1.Provider.DataRequest (Filteredit.Text); END; 11.5.1 Talking directly to the Data property assignment, customer programs can either IProvider The interface acquires data or acquires data from another data set, which is assigned by the DATA property. The program example is as follows: ClientDataSet1.data: = clientdataSet2.data; once the DATA is assigned, you can display this data with a standard data control. Note: When data is acquired from another data set, the log of another dataset will also be copied, but does not include the original range and filter conditions.

If you want to obtain data from another BDE-based data, you can use the data set of the provike, the program example is as follows: ClientDataSet1.Data: = Table1.Provider.data; if you want to get data from a custom datcher, first To create a temporary TPROVIDER component, then set its DataSet property to specify this custom dataset. Sample examples are as follows: Tempprovider: = tdataSetProvider: = tdataSetProvider.create (form1); TempProvider.DataSet: = SourceDataSet; ClientDataSet1.Data: = TempProvider.data; TempProvider.free; 11.5.2 Adding custom information in packets can be customized The information is added to the packet. These custom information will also be saved to files or streams when data is stored in a file or stream. These custom information will also be copied if the packets are assigned to another data set. To add custom information to your packet, you can call the SetOptionalParam function. To retrieve custom information from a packet, you can call GetOptionalParam. Program example follows: Procedure TAppServer.Provider1UpdateData (Sender: TObject; DataSet: TClientDataSet); varWhenProvided: TDateTime; BeginWhenProvided: = DataSet.GetOptionalParam ( 'TimeProvided'); ... End; 11.5.3 TClientDataSet clone call of another data set The CloneCursor function can get the identical copy of a dataset. It is distinguished from the DATA attribute assignment. One of the differences: Data is shared between two data sets, modify one of them will change the other. Difference Two: In addition to data, the CloneCursor function also copies some properties and events, depending on how the reset and keepsettings parameters are set. The CloneCursor function needs to pass three parameters, where the Source parameter specifies the source dataset, the reset parameter, and the KeepSettings parameter are used to set whether the following properties and events are replicated in addition to the data: Filter, Filtered, FilterOptions, ONFilterRecord, IndexName, Mastersource, Masterfields , Readonly, RemoteServer, ProviderName, Provider. If the Reset and KeepSettings parameters are set to false, the above properties and events of the source dataset will be copied to the target dataset. If the reset parameter is set to True, the above properties and events of the target dataset will be emptied. If the reset parameter is set to false, the KeepSettings parameter is set to true, the above properties and events of the target dataset are unchanged, however, these attributes and events must be compatible with the cloned data. 11.6 Communication with Application Server In the multilayer architecture, the client is exchanged with the application server via the iProvider interface. This chapter describes how to get an iProvider interface to the client, how to deliver parameters to the application server, how to write the user to the database to the database. 11.6.1 How to get an iProvider interface in a single-layer application and working in a multi-layer application in the "Briefcase" mode, no need to use the iProvider interface.

In the multilayer architecture, the client program is to exchange data with the application server, first of all, IPROVIDER interfaces must be obtained, which is used to use the RemoteServer attribute and the ProviderName property. The RemoteServer property is used to specify the MIDAS connection component of the client. The MIDAS connection component is also known as Data Broker for establishing and maintaining the connection to the application server. After the RemoteServer property is set correctly, you can select a value for the ProviderName property in the object viewer. It is actually a TPROVIDER component on the application server. 11.6.2 Passing parameter client to the application server can pass parameters to the application server, which is actually transmitted to the TQUERY component or TSTOREDPROC component on the application server. You can also set parameters during the design period. In the design period, you can click the I omitted button on the edge of the Params property to open an editor shown in Figure 11.2. Figure 11.2 Setting parameters Click the button to add a parameter, click the button to delete a parameter, click the button to move a parameter forward, click the button to move a parameter behind. Select a parameter, the object viewer will display the properties of the parameter (TPARAM object). Creating a parameter can be invoked at the running period. CreateParam function can be called to create a parameter. For example, the following code creates a parameter called Custno, and its use type is PTINPUT, the data type is ftinteger, which is set to 605. WITH ClientDataSet1.Params.CreateParam (ftinteger, 'Custno', PTINPUT) DOASINTEGER: = 605; After setting the parameter, if the TclientDataSet's Active property is false, as long as the Active property is set to TRUE, these parameters will be automatically passed to the application server. . If the Active property is already true, call the SendParams function to pass the parameters to the application server. Note: Parameters passed to the application server must match the parameters of the TQuery component or TSTOREDPROC component, including name, data type, and parameter type. 11.6.3 How to request data TclientDataSet to the application server and three methods for requesting data to the application server: First, the fetchondemand property. If this property is set to true, TclientDataSet automatically retrieves additional packets, such as the value of the BLOB field, or the contents of the nested table as needed. If this property is set to False, the program needs to be explicitly calling getNextPacket to get these additional packets. The second is the PacketRecords property that sets up to the maximum number of records in a packet, set to -1 to indicate all records of a data set to accommodate the data set. The third is the GetNextPacket function, used to retrieve the next packet to the application server, and add the retrieved packet to the previously retrieved packet. This function returns the number of records actually retrieved. The fourth is the Fetchblobs process, which is used to retrieve the value of the BLOB field from the application server. If the fetchondemand property is set to true, it is not necessary to call the fetchblobs function. The fifth is the FetchDetails process, which is used to retrieve data in the nested table. If the Fetchondemand property is set to true, it is not necessary to call the fetchDetails function. 11.6.4 Updating the database In the multilayer architecture, the user needs to write the latest data to the database after the client is modified, which is to call the ApplyUpdates function of the TclientDataSet.

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

New Post(0)