Multi-layer database development 7: word segment object

Source: Internet
Author: User
Chapter 7 CIDR
Delphi 4 uses tfield to manipulate fields in the dataset. However, the specific fields are actually derived classes of tfield. Therefore, tfield is rarely directly used in applications.
When you open a dataset, Delphi 4 automatically generates dynamic field objects related to the data type. Of course, you can use the field editor to create a permanent field object instead of a dynamic field object.
7.1 specific field object
Delphi 4 supports 24 types of fields inherited from tfield, including:
. Tadtfieldoracle8 ADT field. ADT is the abbreviation of abstract data type;
. Taggresponfieldtclientdataset automatically maintained total fields;
. Tarrayfield array field;
. The automatic growth field in the tautoincfieldparadox table, ranging from-2,147,483,648 to 2,147,483,647;
. Tbcdfieldbcd field;
. Tbooleanfield Boolean field;
. Tblobfield binary data, theoretical length 2 GB;
. Tbytesfield binary data, theoretical length 2 GB;
. Tcurrencyfield: real number field. The value ranges from 5.0e-324 to 1.7e308;
. Tdatasetfield indicates the fields of the nested table;
. Tdatefield Date field;
. Tdatetimefield Date and Time fields;
. Tfloatfield floating point field, value range: 5.0*10e-324 to 1.7*10e308;
. Tbytesfield binary data, up to 255 bytes;
. Tintegerfield integer field, value range:-2,147,483,648 to 2,147,483,647;
. Tlargeintfield long integer field, value range:-263 to 263;
. Tmemofield remarks field, theoretical length 2 GB;
. Tnumericfield real number, value range from 3.4 E-4932 to 1.1e4932;
. Treferencefield: a pointer to reference other datasets;
. Tsmallintfield integer field, value range:-32,768 to 32,768;
. Tstringfield string field. The maximum number of bytes is 8192, including null characters;
. Ttimefield time field;
. Tvarbytesfield binary data. The maximum number of bytes is 255;
. Twordfield unsigned integer, value range: 0 to 65,535.
7.2 understand the Field object
Just like Data Access Components such as tTable and tquery in Delphi 4, tfield is not visible and cannot be seen directly during the design period, you can only access it indirectly through Data Access Components and data controls.
The Field object created in Delphi 4 is related to the Data Type of the field. For example, for a string field, the tstringfield object is created. For floating-point fields, the tfloatfield object is created. The following uses tfloatfield as an example to illustrate how to manipulate the display attribute of a field:
. Alignment is used to specify the Data Alignment mode, such as left alignment, center alignment, and right alignment;
. Displaywidth is used to specify the display width (in characters) of data );
. Displayformat is used to specify the display format of data, such as the decimal place;
. Editformat specifies the format of the data displayed during data editing.
For different field objects, most of their attributes are the same, but some attributes are dedicated. For example, the precision attribute of tfloatfield is not available for other field objects. Most of the attributes of a Field object only affect the display attributes of data on the form, but some attributes such as precision also affect user input and Data Format modification.
In an open dataset, all field objects are either dynamic field objects or permanent field objects. The following describes these two different types of field objects in detail.
7.3 dynamic field object
By default, when a dataset is placed on the data module and opened, Delphi 4 automatically generates a dynamic field object for each field in the dataset. It is dynamic, on the one hand, because it is automatically generated, on the other hand, because it always reflects the situation of the physical dataset, for different types of fields, the types of generated field objects are also different. If the structure or other information of the dataset changes, when the application re-opens the dataset, all the field objects are rebuilt based on the latest structure and information.
The lifetime of the dynamic field object is temporary. When the dataset is closed, these field objects also disappear.
It can be seen that the biggest feature of dynamic field objects is adaptability. With this feature, you can write some common database applications that can open different datasets. To use a dynamic field object in an application, you must first put a dataset component on a form or data module, and then put a tdatasource component on a form or data module, set its dataset attribute to specify the dataset component. Place a Data Control on the form and set its datasource attribute to specify the tdatasource component. If necessary, specify the field to be displayed. Finally, set the active attribute of the dataset component to true. When the dataset is opened, a dynamic field object is automatically created.
The disadvantage of a dynamic field object is that you need to write code to change the display attribute and data format of a field. Even if you are willing to write code, you cannot change the display sequence of fields in any way. You cannot temporarily hide some fields or add new fields such as "calculated field" and "lookup field ". In addition, the field data type cannot be changed. Therefore, in many cases, applications often use permanent field objects instead of dynamic field objects.
7.4 permanent field object
The biggest benefit of replacing a dynamic field object with a permanent field object is that its attributes can be set during the design period. In addition, permanent field objects have the following advantages:
. You can select some fields;
. You can add new fields, including "calculated fields" and "lookup fields ";
. You can change the data type of the original field.
7.4.1 how to create a permanent field object
To create a permanent field object, you must use the field editor. The permanent field object can ensure that each time the application runs, the user will always see these fields, even if the dataset structure has changed. In this way, no matter the data control or program code, you do not have to worry about the data control and program code being unable to adapt to the change in the structure of the dataset. However, if the source field of a permanent field object is deleted, an exception is triggered.
To create a permanent field object, follow these steps:
The first step is to place a dataset component such as Ttable on the data module, set the databasename attribute to specify the database to be accessed, and set the tablename attribute to specify the table to be accessed.
Step 2: double-click the tTable component. Delphi 4 will open the field editor, as shown in 7.1.
Figure 7.1 field editor
The field editor consists of a title bar, a navigation button, and a checkable field list.
The title bar displays the name of the dataset, including the name of the form or data module where the dataset is located. For example, if the dataset is named MERs and placed on customerdata, the title bar displays customerdata. Customers.
The four navigation buttons are used to browse the records of a dataset. They can flip a record forward, a record backward, the first record, and the last record. If the active attribute of the dataset is set to false or the dataset is empty, the four navigation buttons are grayed out.
The navigation button is a checkable list box listing all permanent fields. When the field editor is started, the list is empty, because by default, Delphi 4 generates dynamic field objects.
Step 3: Right-click and select the "add fields" command in the pop-up menu. The "addfields" dialog box is displayed, as shown in Figure 7.2.
Figure 7.2 Select fields
Select one or more fields and click OK. Delphi 4 creates permanent field Objects Based on the fields you selected. The names of these permanent field objects are displayed in the field editor, as shown in 7.3.
Figure 7.3 list the names of permanent field objects
The navigation buttons are gray, probably because the dataset is not opened. As long as a permanent field object is created, Delphi 4 will not create a dynamic field object each time a dataset is opened. For the same dataset, the dynamic field object and the permanent field object cannot exist at the same time.
Each time you open a dataset, Delphi 4 checks whether the fields of each permanent field object exist in the dataset. If the fields do not exist, Delphi 4 triggers an exception and rejects the dataset.
Once a permanent field object is generated, You can regard it as a component, set its properties in the object observer, and create an event handle. In fact, a permanent field object is a component.
For those data controls, as long as a permanent field object is created, only the fields with a permanent field object can be displayed by the Data Control.
After creating a permanent field object, you can find the declaration of this field object in the unit file, for example:
Typetform1 = Class (tform)
Table1: tTable;
Button1: tbutton;
Table1xxh1: tstringfield;
Table1xxh2: tbytesfield;
Procedure button1click (Sender: tobject );
Private {private Declarations}
Public {public declarations}
End;
7.4.2 adjust the sequence of permanent fields
The sorting order of permanent field objects in the field editor is the display order of these fields in the Database Grid (TDBGrid. Therefore, to change the display order of fields, you can change the arrangement order of permanent field objects in the field editor. The adjustment method is to select a field first, then drag it to another position with the mouse. You can also press Ctrl + up to move the field up sequentially, and press Ctrl + DN to move the field down sequentially.
If multiple fields are selected, and these fields are not consecutive, they are dragged to a new position, and these originally discontinuous fields are now continuous.
7.4.3 add new fields
Another advantage of using a permanent field object is that you can add new fields or replace the original fields. Delphi 4 allows adding five types of fields:
. Data is used to replace the original field. For example, you can change the data type of the field;
. Calculated adds a "calculated field". The value of this field is provided by the oncalcfields event;
. Internalcalc is similar to "computing field" and is used in customer programs with multi-level architecture;
. Lookup adds a "lookup" field;
. Aggregate adds a "total field ".
The preceding five types of fields are only used for display. Their data is only valid at runtime and does not affect the original dataset.
To add a new field, right-click the field editor and choose "New Field" from the shortcut menu. The "New Field" dialog box is displayed, as shown in Figure 7.4.
Figure 7.4 create a field dialog box
The new field dialog box consists of the field properties grouping box, fieldtype grouping box, and lookup definition grouping box.
The "field type" group box contains three radio buttons to specify the generation type (not the data type) of the newly created field. The default type is "data ", if you select "lookup", the "lookupdefinition" grouping box below will be activated. You can also select "calculated" to add a "calculated field ".
If the dataset is tclientdataset, two radio buttons are added to the Field Type box, "internalcalc" and "aggregate ".
The "field properties" grouping box is used to set the attributes of a field. The "name" box is used to enter the field name, delphi 4 generates the name of the Field object in the "component" Box Based on the input name field. The "type" box is used to select the Data Type of the field. Delphi 4 supports generating ADT fields. The "size" box is used to enter the length of a field. For fields of the string, bytes, and varbytes types, the length must be entered. For fields of other types, the length is not required.
7.4.4 Add "data" Fields
Add a "data" field to replace an existing field, especially the Data Type of the field. For example, assume that the type of a field is tsmallintfield and you want to change it to tintegerfield. Because programming cannot change the data type of a field, you can only delete this field first, add a "data" field and set its data type to integer.
The general steps for adding a "data" field are as follows:
The first step is to delete the permanent field to be replaced in the field editor and remember its name.
Step 2: Right-click and select "new field" from the menu to bring up the "Newfield" dialog box. Enter the name of the field in the "name" box, which must be exactly the same as the name of the field to be replaced, so that you can obtain data from the original field.
The third step is to set the data type and length of the new field, which must be different from the original data type. It is not enough to change the length of a string field. Note that the data types must be different, and the new data types must be compatible with the original data.
Step 4: Select "data" in the "field type" group box and click OK. At this point, a new field is created, which replaces an existing field.
7.4.5 Add "calculated" Fields
The calculated field value is provided in the handle for processing the oncalcfields event. The general steps for adding a "calculated" field are (the previous common steps are omitted ):
The first step is to enter the name of the field in the "name" box. It cannot be the same as the name of an existing field.
Step 2: select the Data Type of the field in the "type" box and specify the Data Length in the "size" box. If the field type is tstringfield, tbytesfield, or tvarbytesfield.
Step 3: Select "calculated" in the "field type" group box and click OK.
Step 4: Click the dataset component on the form or data module, create a handle to handle the oncalcfields event, and provide the value of "calculated field" in the handle. The program example is as follows:
Citystatezip. Value: = city. Value + ',' + state. Value + ''+ Zip. value;
If the handle for oncalcfields event processing is not created, or the "calculated field" value is not provided in the handle, the "calculated field" value is empty.
It should be noted that if the dataset is tclientdataset or tquery, you can also add a field of the "internalcalc" type. Fields of the internalcalc type are very similar to those of the calculated type. The difference is that for tclientdataset, the values of fields of the internalcalc type are accessed along with the values of other fields.
7.4.6 Add "lookup" Fields
The so-called "lookup" type field is that its value can only be obtained from another data set.
The general steps for adding a "lookup" field are (the previous common steps are omitted ):
The first step is to enter the name of the field in the "name" box. It cannot be the same as the name of an existing field.
Step 2: select the Data Type of the field in the "type" box and specify the Data Length in the "size" box. If the field type is tstringfield, tbytesfield, or tvarbytesfield.
Step 3: Select "lookup" in the "field type" group box. The "lookupdefinition" group box below will be activated.
Step 4: select a dataset in the "dataset" box of the "lookup definition" grouping box. The dataset cannot be the dataset where the "calculated field" is located. Otherwise, an exception is triggered.
Step 5: select a field as the key field in the "Key Fields" box of the "lookup definition" grouping box. If you want to use several fields as the key field, you can enter the names of these fields in the "Key Fields" box and separate them with semicolons. Fields used as keyword fields must have permanent field objects.
Step 6: select a field in the "lookup keys" box of the "lookup definition" grouping box as the key field to match the field selected in step 5. Note: The fields in the specified dataset in the "dataset" box are listed here. If you want to use several fields as key fields, you can directly enter the names of these fields in the "lookup keys" box and separate them with semicolons.
Step 7 is to select a field in the "result field" box in the "lookup definition" grouping box. The value of this field will be returned to the newly added "lookup" type field.
If "calculated field" and "lookup" are defined, the value of the "lookup" field is determined first, in the handle for processing the oncalcfields event, you can reference the value of a field of the "lookup" type.
Tfield has a lookupcache attribute. For fields of the "lookup" type, if this attribute is set to true, when the data set is opened for the first time, put the value of the "lookup" type field in the cache, and then you can directly read the value of this field from the cache without retrieving it from another dataset. Obviously, this can improve application performance, especially when another dataset is located on a remote server.
However, if too many fields are specified in the "keyfields" box, it is best to set the lookupcache attribute to false.
Note: If you set the lookupcache attribute to true at runtime, you should call refreshlookuplist to initialize the cache.
Here, we will introduce a technique to provide data for fields of the "lookup" type through programming, so as to avoid getting data from another data set. Create an object instance of tlookuplist and call add to create a data list. Then, set the lookuplist attribute of the field to specify the object instance of tlookuplist, and set the lookupcache attribute to true. Then, the value of the "lookup" type field will be read from the list.
7.4.7 Add "aggregate" Fields
You can only add "aggregate" fields to a dataset created in tclientdataset. The general steps are as follows:
The first step is to enter the name of the field in the "name" box. It cannot be the same as the name of an existing field.
The second step is to select the Data Type of the field in the "type" box. Generally, select "aggregate ".
Step 3: Select "aggregate" in the "field type" group box and click OK.
Step 4: Click the newly created "aggregate" field in the field editor and set the expression attribute to specify an expression.
7.4.8 delete a permanent field object
To delete a permanent field object, open the field editor, select the field to be deleted, and press the Del key. You can also right-click the field to be deleted and select the "delete" command in the pop-up menu.
Once a permanent field object is deleted, the application will no longer be able to access it. If you want to restore it later, you can only create a new permanent field object, and the original attributes and event handles are lost.
If you delete all permanent field objects, dynamic field objects will be automatically created the next time you open the dataset.
7.5 set attributes of a permanent field object
One advantage of using a permanent field object is that you can set its properties and create an event handle during the design period. For example, you can set the display width of a field and whether its value can be modified.
To set attributes of a permanent field object and create an event handle, you must first open the field editor, select a field in the field editor, and then perform related operations in the object observer.
7.5.1 set the display and edit attributes of fields
Tfield provides several attributes that are listed as follows:
. Alignment sets the alignment of fields in the Data Control (left, center, right );
. Constrainterrormessage: the information specified by this attribute is displayed when the user's input violates the error correction rules;
. Customconstraint sets a local error correction rule;
. Set currency to true to display data in currency format;
. Displayformat: Set the display format of fields in the Data Control;
. Displaylabel: sets the column label of the field in the Database Grid;
. Displaywidth: Set the display width of a field in the Data Control;
. Editformat sets the display format of the field during editing;
. Editmask sets the rules that users must follow when editing data;
. Fieldkind specifies the field generation type;
. Fieldname specifies the field name;
. Hasconstraints if the field already has an error correction rule, this attribute returns true;
. Importedconstraint: Error Correction rules returned from the server or data dictionary;
. Index specifies the serial number of the field in the dataset;
. Lookupdataset: specify another dataset to search for the field values;
. Lookupkeyfields specifies one or more fields to match the keyword fields;
. Lookupresultfield specifies a field. The value of this field will be copied to the lookup field;
. Maxvalue specifies the maximum value of a field;
. Minvalue specifies the minimum value of the field;
. Name specifies the internal name of the permanent field object, for example, table1xxh;
. Origin: the original name of the returned field in the dataset;
. Precision specifies the valid bit of the field;
. If readonly is set to true, the field value is read-only;
. Size specifies the length of the field (in characters );
. Tag to give each Field object a identification number;
. If transliterate is set to true, the conversion will be performed according to the driver;
. Setting visible to false indicates that this field does not appear in the Data Control.
It should be noted that some attributes do not apply to certain types of fields. For example, for a tstringfield field, it does not have the attributes such as currency, maxvalue, and displayformat, for a tfloatfield field, it does not have the size attribute.
The preceding attributes can also be set at runtime. For example, if you want to set the readonly attribute of the citystatezip field of the MERs table, write it as follows:
Customerscitystatezip. readonly: = true;
Note: you cannot use the field name to access the above attributes, but use the field object name to access the attributes.
7.5.2 set the user input format
The editmask attribute is only applicable to fields of the tstringfield, tdatefield, ttimefield, and tdatetimefield types. It is used to set the user input format.
To set the editmask attribute, click the ellipsis next to the editmask attribute in the object observer to open the "input mask Editor" dialog box, as shown in 7.5.
Figure 7.5 setting editmask Attributes dialog box
You can select an existing format or custom format in the "sample Masks" box. Click "Masks" to select more formats.
7.5.3 use the default display format
For fields of the tfloatfield, tcurrencyfield, tintegerfield, tsmallintfield, twordfield, tdatefield, tdatetimefield, and ttimefield types, Delphi 4 provides several routines to display these fields in the default format. These routines are declared in the sysutils unit, including:
. Formatfloat is suitable for tfloatfield and tcurrencyfield;
. Formatdatetime is suitable for tdatefield, ttimefield, and tdatetimefield;
. Formatcurr is suitable for tcurrencyfield.
The default format is actually set in "Control Panel. You can open "Control Panel" and double-click the "region Settings" icon, and you will see that the format of numbers, currencies, times, and dates is set. For example, if the region is United States, For fields of the tfloatfield type, the display format is $1234.56.
Of course, You can reset the display format of the displayformat attribute and editformat attribute specified fields during the design or runtime. You can also customize the display format of fields in the handles for ongettext and onsettext events.
7.5.4 handle events
Like most components, permanent field objects also have events. You can create event handling handles during the design period. The tfield events are listed below:
. Onchange triggers this event when the field value changes;
. Ongettext this event is triggered when the program attempts to retrieve the value of a field;
. Onsettext this event is triggered when the program tries to set the field value;
. Onvalidate this event is triggered when the field value is to be written to the record buffer.
The ongettext and onsettext events are used to set the display format of fields. The onchange event is used to respond to data changes, such as disabling or allowing a menu item. The onvalidate event verifies the field value.
7.5.5 call Method
The following method is used to list tfield:
. Assignvalue is used to assign values to fields. It automatically performs type conversion;
. Clear clears the field value to NULL;
. Getdata writes unformatted data to a buffer;
. Isvalidchar determines whether a character is valid for a field;
. Setdata assigns unformatted data in the buffer to the field;
. Focuscontrol moves the input focus to the data control where the field is located. If several data controls on the form display the value of a field, focuscontrol moves the input focus to the first data control that displays the field.
7.6 Property sets
If the display attributes of multiple fields are the same or roughly the same, you do not need to repeat them. You can set attributes of a field, such as alignment, displaywidth, displayformat, maxvalue, and minvalue, create an attribute set and add it to the data dictionary. In this way, other fields can introduce attribute sets from data fields.
To create an attribute set, first open the field editor, select a permanent field, and then set the relevant attributes in the object observer. After setting, right-click the field editor and choose Save attributes from the shortcut menu. The save attributes as dialog box is displayed, as shown in 7.6.
Figure 7.6 save an Attribute Set
By default, the attribute set name is the table name and field name, for example, customcompany, which can be changed.
You can also create an attribute set in SQL explorer. The advantage of creating an attribute set in SQL explorer is that you can specify the field type and specify a data control such as tdbedit and tdbcheckbox, when a field based on this attribute set is dragged to the form, the Data Control is automatically added to the form.
After an attribute set is created, other fields can introduce this attribute set. First, open the field editor, select the field to introduce the attribute set, right-click the field, and select the "associate attributes" command in the pop-up menu. The "associate attributes" dialog box is displayed, 7.7.
Figure 7.7 introduce an Attribute Set
Select an attribute set and click OK. The attribute in this attribute set will be applied to the selected field. This is called introducing the attribute set.
If you do not want to use the attributes in this attribute set in the future, you must open the field editor, select a field, right-click the field, and select "unassociate attributes" in the pop-up menu ".
7.7 display, conversion, and Access Field Values
The data control can automatically display the field values. When editing is allowed, you can modify the Field Values in the Data Control and write the modified values back to the dataset, no code is required.
The application can access the value of a field through the value attribute of tfield. For example, the following line of code displays the value of the customerscompany field in the edit1 edit box:
Edit1.text: = customerscompany. value;
Theoretically, the value attribute can access the values of fields of any data type. However, many data controls can only pass string-type values. Therefore, you need to convert the value attribute.
Fortunately, tfield provides many conversion functions to access the value of a field based on a specific data type. For example, asstring can be used to access numeric or Boolean fields by string type.
Asvariant applies to all data types. It should be noted that the conversion is not always successful. For example, asdatetime can convert a string to date and time, but the string must be recognized as a date and time format, it cannot be a common string. In some cases, although the conversion can be performed, the precision may be lost.
You need to know the name of the Field object before using the value attribute to access the field value. In many cases, you often do not know the name of the Field object, but the field name is known. Therefore, applications often access the value of a field through the serial number or name of the field.
The dataset component has a fields attribute. This is an array. Each element of the dataset represents a field. In this way, you can access the value of a field by sequence number, starting from 0. For example, the following code displays the 7th fields in the MERs table in the edit1 edit box:
Edit1.text: = custtable. Fields [6]. asstring;
The following code sets the value of the 7th fields to the content in the edit1 edit box:
Customers. Edit;
Customers. Fields [6]. asstring: = edit1.text;
Customers. post;
However, it is not safe to access the field value by serial number, because if the serial number is wrong, the correct result will not be obtained. Therefore, we recommend that you access the field value by field name, which requires the fieldbyname function.
To call the fieldbyname function, you must pass the field name as a parameter to the fieldbyname function. For example, the following code shows the custno field of the MERs table to edit2 in the editing box:
Edit2.text: = customers. fieldbyname ('custno'). asstring;
The following code sets the value of the custno field to the content in the edit2 edit box:
Customers. Edit;
Customers. fieldbyname ('custno'). asstring: = edit2.text;
Customers. post;
In a multi-tier architecture application, an error may occur when the client requests data update from the application server. In this case, you can use the curvalue attribute to check the current value of the field, because the field value can be modified at any time, especially in a multi-user environment, it is necessary to know the current value of the field.
7.8 field-level error correction
The so-called field-level Error Correction Means to define some rules or conditions in advance, and the field value must comply with these rules or conditions, otherwise it will be considered as an error. The Field object can be used by the SQL Server for error correction or custom error correction.
Most SQL servers define the error correction function, while client programs can use the server for error correction. The importedconstraint attribute of tfield can return the server's error correction rules. It is actually a string and Its syntax is similar to an SQL statement, for example:
Values> 0 and value <100
If the server's error correction still does not meet the requirements, you can use the customconstraint attribute to customize the error correction rules. This attribute is different from the importedconstraint attribute. If the server-side error correction rule changes, the importedconstraint attribute also changes, while the custom error correction rule remains unchanged.
Custom Error Correction only applies to this application and only verifies the data entered by the user. It does not support data sent to or received from the application server.
To customize an error correction rule, you must use the customconstraint attribute to specify an error correction rule and set the constrainterrormessage attribute to specify an information. This information is displayed when the field value violates the error correction rule. The custom error correction rules are similar to the where part of an SQL statement. For example, x> 0 and x <100. X is only a symbol used to reference the field value, the symbols themselves are irrelevant, as long as they are consistent in the error correction expression.
7.9 Oracle 8 object fields
Delphi 4 is the best tool to use the Oracle 8 object Association function. It supports four latest fields, including ADT (abstract data type), array, dataset (nested table), and reference, their field types are listed as follows:
. Tadtfield represents an ADT field;
. Tarrayfield indicates an array field;
. Tdatasetfield indicates a dataset field;
. Treferencefield indicates a reference field pointing to another ADT field.
The above four types of fields are collectively referred to as object fields. One of their basic features is that they contain or reference another field. If a dataset contains object fields, a permanent field object is automatically created for the Child fields contained or referenced by these object fields during the design period. Meanwhile, the objectview attribute of the dataset component is set to true automatically. When the objectview attribute is set to true, fields in the dataset are stored in a tree based on their inheritance relationships, rather than linear storage based on the original sequence number.
Some attributes and methods specific to an object field are listed below, which are used to manipulate the child fields it contains or references:
. Fields can use this attribute to access the child fields contained or referenced by the object field;
. Objecttype: Type of the returned object;
. Fieldcount returns the number of child fields contained or referenced by the object field;
. Fieldvalues uses this attribute to access the values of child fields contained or referenced by object fields.
7.9.1 ADT Field
ADT is a user-defined field type, which is somewhat similar to the structure. It can contain most types of fields, including arrays, references, and nested tables.
There are several ways to access an ADT field. It is best to create a permanent field object first. Assume that an ADT field is called address and contains four sub-fields: Street, city, state, and zip. If a permanent field object is created for the address field, the following permanent field objects are displayed:
Customeraddress: tadtfield;
Customeraddrstreet: tstringfield;
Customeraddrcity: tstringfield;
Customeraddrstate: tstringfield;
Customeraddrzip: tstringfield;
In this way, you can use the child field of the object field as a common field, for example:
Cityedit. Text: = customeraddrcity. asstring;
However, the code above assumes that the permanent field object of the object field has been created. If the permanent field is not created, the following method is used to access the child field of the object field:
Cityedit. Text: = Customer. fieldbyname ('address. City'). asstring;
You can use the fieldvalues attribute of tadtfield to access the value of a subfield. For example:
Cityedit. Text: = tadtfield (customer. fieldbyname ('address') [1];
Because the fieldvalues attribute is the default attribute of tobjectfield, the above Code can be changed:
Cityedit. Text: = tadtfield (customer. fieldbyname ('address'). fieldvalues [1];
The above code can also be changed to the fields attribute, for example: cityedit. Text: = tadtfield (customer. fieldbyname ('address'). Fields [1]. asstring;
You can even write cityedit. Text: = tadtfield (customer. fieldbyname ('address'). Fields. fieldbyname ('city'). asstring;
It can be seen that it is the easiest to access the sub-fields of object fields through permanent field objects. In addition, the ADT field can be accessed through the fieldvalues attribute of the dataset component, for example:
Customer. Edit; customer ['address. City']: = cityedit. Text; customer. post;
The following code displays the value of the city subfield of the address field in the cityedit edit box:
Cityedit. Text: = Customer ['address. City'];
7.9.2 array Field
The so-called array field is similar to an array. It contains the same type of sub-fields, and the types of sub-fields contained in the ADT field can be different. The subfield of an array field can be a common field or an ADT field, but cannot be another array field.
There are several methods to access the array fields in the dataset. The following is a typical access method:
VaR
Orderdates: tarrayfield;
I: integer;
Begin
For I: = 0 to orderdates. Size-1 do
Begin
If orderdates. Fields [I]. Is null then break;
Orderdatelistbox. Items. Add (orderdates [I]);
End;
End;
Suppose there is an array field called telnos_array, which has 6 string-type subfields. When a permanent field object is created for the telnos_array field, a permanent field object with six child fields will be created:
Customertelnos_array: tarrayfield;
Customertelnos_array0: tstringfield;
Customertelnos_array1: tstringfield;
Customertelnos_array2: tstringfield;
Customertelnos_array3: tstringfield;
Customertelnos_array4: tstringfield;
Customertelnos_array5: tstringfield;
The following code accesses the first child field:
Teledit. Text: = customertelnos_array0.asstring;
If a permanent field object is not created for an array field, you can only use the fieldvalues attribute to access the value of the Child field. However, you must set the objectview attribute of the dataset component to true. For example:
Teledit. Text: = tarrayfield (table1.fieldbyname ('telnos _ array'). fieldvalues [1];
Because the fieldvalues attribute is the default attribute of tobjectfield, the above Code can be changed:
Teledit. Text: = tarrayfield (table1.fieldbyname ('telnos _ array') [1];
The preceding code can also be written as follows: teledit. Text: = tarrayfield (customer. fieldbyname ('telnos _ array'). Fields [1]. asstring;
7.9.3 dataset Fields
Using dataset fields, you can easily maintain one-to-multiple relationships between tables. You can use the nesteddataset attribute of tdatasetfield to access the child dataset nested in the dataset field.
To support dataset fields, the TDBGrid component of Delphi 4 has been rewritten. All columns that display dataset fields have a ellipsis button. You can click this ellipsis to open a new window, this window also uses a TDBGrid component to display the data of the nested table. To open this window, you can also call the showpopupeditor function of TDBGrid, for example:
Dbgrid1.showpopupeditor (dbgrid1.columns [7]);
To access data in a nested table, it is best to create a permanent field object for the dataset field, and then specify this field using the datasetfield attribute of tnestedtable or tclientdataset.
7.9.4 reference fields
The referenced field expresses and stores a pointer pointing to another ADT object, which is usually a record in another dataset. The data in the referenced ADT object can be accessed like a nested table, or through the fields attribute of treferencefield.
Similar to dataset fields, in the TDBGrid component, all columns that display referenced fields have a ellipsis. You can click this ellipsis to open a new window, this window also displays the data in the referenced ADT object in a grid. To open this window, you can also call the showpopupeditor function of TDBGrid. For example:
Dbgrid1.showpopupeditor (dbgrid1.columns [7]);
To access data in the referenced ADT object, you must first create a permanent field object for the referenced field, and then specify this field using the datasetfield attribute of tnestedtable or tclientdataset.
Assume that a referenced Field object is mermerrefcity. The following code accesses a field in the ADT object referenced by it. The two lines of code have the same effect.
Cityedit. Text: = customerrefcity. Fields [1]. asstring;
Cityedit. Text: = customerrefcity. nesteddataset. Fields [1]. asstring;
To assign a value to the referenced field, you must use the SQL SELECT statement. The program example is as follows:
VaR
Addressquery: tquery;
Customeraddressref: treferencefield;
Begin
Address. SQL. Text: = 'select REF (a) from addresstable A where a. City = "San Francisco "';
Addressquery. Open; customeraddressref. Assign (addressquery. Fields [0]);
End;
7.9.5 display ADT fields and array Fields
The ADT field and the array field share the same characteristics. They both contain subfields. Delphi 4 has modified some data controls, such as tdbedit and TDBGrid, which support ADT fields and array fields.
For data controls with the datafield attribute, when they display the values of the ADT field and the array field, the values of their child fields are automatically displayed in a drop-down list.
How TDBGrid displays the values of the ADT field and array field depends on how the objectview attribute of the dataset component is set. If the objectview attribute is set to false, each subfield is displayed in the column. If the objectview attribute is set to true, a drop-down arrow is displayed in the ADT field and array field columns. Clicking this arrow expands the grid to display the values of all child fields.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.