Multi-layer database development seven: field object

xiaoxiao2021-03-06  104

Seventh chapter field pair like Delphi 4 with TField to manipulate fields in the data set. However, the specific fields are actually a derived class of Tfield, so applications are rarely used directly to TField.

When a dataset is opened, Delphi 4 automatically generates dynamic, field objects related to the data type. Of course, you can create a permanent field object with a field editor instead of dynamic field objects.

7.1 Specific field object

Delphi 4 supports 24 types of fields, which are inherited from TField, including:

.Tadtfieldoracle8's ADT field. ADT is abbreviation from Abstract Data Type;

.TaggRegatefieldTClientDataSet Total field;

.Tarayfield array field;

The automatic growth field in the .TautoIncfieldparadox table, the range range from -2, 147, 483, 648 to 2, 147, 483, 647;

.Tbcdfieldbcd field;

.Tbooleanfield boolean field;

.TBLOBFIELD binary data, theoretical length 2GB;

.Tbytesfield binary data, theoretical length 2GB;

.Tcurrencyfield real-digit field, value range from 5.0E-324 to 1.7E308;

.TDataSetfield represents the field of the nested table;

.Tdatefield date field;

.Tdatetimefield date and time field;

.Tfloatfield floating point field, value range from 5.0 * 10E-324 to 1.7 * 10E308;

.Tbytesfield binary data, maximum number of bytes 255;

.Tintegerfield integrity field, value range from -2, 147, 483, 648 to 2, 147, 483, 647;

.TLARGEINTFIELD long set field, range value range from -263 to 263;

.Tmemofield note field, theoretical length 2GB;

.Tnumericfield real number, value range from 3.4 E-4932 to 1.1E4932;

.Treferencefield a pointer to other data sets;

.Tsmallintfield integrity field, range from -32, 768 to 32, 768;

.Tstringfield string field, maximum number of bytes 8192, including NULL characters;

.Ttimefield time field;

.Tvarbytesfield binary data, maximum number of bytes 255;

.Twordfield no symbol integer, value range from 0 to 65, 535.

7.2 Understanding Field Objects

Just like the Delphi 4 data access components such as TTABLE, TQUERY, TField is also non-visual, even can't see it directly on the design period, and can only access it indirectly through data access components and data controls.

The field object created by Delphi 4 is related to the data type of the field, for example, for a string type field, created a TSTRINGFIELD object. For floating-point types of fields, created a TFLoatField object. Let's take a TFLOATFIELD as an example to see how to manipulate the display properties of the field:

.Alignment is used to specify the alignment of the data, such as left alignment, center, right right;

.Displaywidth is used to specify the display width of the data (in units of characters);

.DisplayFormat is used to specify the display format of the data, such as the position of the decimal point;

.EditFormat Specifies the format of displaying data when editing data.

For different types of field objects, most of their properties are the same, but there are some properties to be dedicated, for example, TFLoatField's precision attribute is not available. Most of the properties of field objects simply affect the display attributes on the form, but some attributes such as Precision also affect user input and modify data. In an open data set, all field objects are either dynamic field objects, either a permanent field object. These two different types of field objects will be described in detail below.

7.3 Dynamic field object

By default, when a data set is placed on a data module and open it, Delphi 4 automatically generates a dynamic field object for each field in the data set. The reason why it is dynamic, on the one hand because it is automatically generated, and on the other hand, it is because it always reflects the case of the physical data set. For different types of fields, the type of field objects is different. If the structure or other information of the data set changes, all field objects are rebuilt based on the latest structural and information when the application reopens this dataset.

The life of the dynamic field object is temporary. When the data set is turned off, these field objects have disappeared.

It can be seen that the maximum feature of dynamic field objects is adaptability. With this feature, you can write some universal database applications that open different datasets. To use dynamic field objects in your application, you first put a data set component on the form or data module, put a TDataSource component on the form or data module, set its DataSet property to specify the data set component. . Place a data control on the form, set its DataSource property to specify the TDataSource component, if necessary, specify which field to display. Finally, set the Active property of the data set component to TRUE to open the dataset, and the dynamic field object is automatically created.

The deficiency of dynamic field objects is that you want to change the display properties of the field, the data format, you need to write code. Even if you are willing to write the code, no matter how you can't change the display order of the field, you cannot temporarily hide some fields, and you can't add new fields such as "calculation field", "lookup field". Moreover, you cannot change the data type of the field. Therefore, in many cases, applications often use permanent field objects instead of dynamic field objects.

7.4 Permanent field object

With a permanent field object instead of dynamic field objects, it is possible to set its properties at design period. In addition, the permanent field object also has the following advantages:

You can choose some fields;

Can add new fields, including "calculation 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, use the field editor. With a permanent field object to ensure that the application always sees these few fields each time you run, even if the structure of the data set has changed. This way, no matter whether the data control or program code does not have to worry because the data control and program code cannot be adapted because the structure of the data set changes. However, if a permanent field object is deleted, an exception is triggered.

The general steps for creating a permanent field object are:

The first step is to put a data set component such as TTABLE on the data module, set the DatabaseName property Specify the database to be accessed, and set the tablename property Specify the table to access.

The second step is to double-click the TTable component, and Delphi 4 will open the field editor, as shown in Figure 7.1.

Figure 7.1 Field Editor

The field editor consists of a list of headlines, navigation buttons, and a check-in list.

The title bar The name of the data set includes the name of the form or data module where the dataset is located. For example, it is assumed that the data set is called Customers, placed on the Data Module CustomerData, and the title bar will display CustomerData.customers. The four navigation buttons are used to browse the records of the data set, they can turn a record forward, turn back a record, turn to the first record and turn to the last record. If the data set of the Active property is set to false or the data set is empty, the four navigation buttons will be grayed.

The navigation button is below a check-in list box listing all permanent fields. When you start opening the field editor, the list is empty because by default, Delphi 4 is generated by dynamic field objects.

The third step is to click the right mouse button. In the pop-up menu, select the "AddField" command to pop up the "AddFields" dialog, as shown in Figure 7.2.

Figure 7.2 Select field

Select one or a few fields and click the OK button. Delphi 4 will create a permanent field object based on the field you selected, and the names of these permanent field objects appear in the field editor, as shown in Figure 7.3.

Figure 7.3 Lists the name of the permanent field object

The navigation buttons are ash, probably because the data set is not open. As long as a permanent field object is created, Delphi 4 will not create a dynamic field object each time you open the dataset. For the same dataset, dynamic field objects and permanent field objects cannot exist at the same time.

Each time you open the data set, Delphi 4 will check if the field based on each permanent field object is still present in the data set. If the field is no longer, Delphi 4 will trigger an exception and refuse to open the data set.

Once a permanent field object is generated, it can be seen as a component, set its properties in the object viewer, establish 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 fields that create a permanent field object can be displayed by the data control.

After you create a permanent field object, you can find the declaration of this field object in unit files, as follows:

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 Adjusting the order of permanent fields

The arrangement of the permanent field object in the field editor is the order in which these fields are in the database grid (TDBGRID). Therefore, to change the display order of the field, you can change the order of the permanent field object in the field editor, the adjustment method is: first select a field, then drag it with the mouse to another, you can press Ctrl Up The key shifts the order of the field and press the Ctrl DN key to move the field order.

If multiple fields are selected, and these fields are not continuous, they drag them to a new location, these discontinuous fields will now become continuous.

7.4.3 Adding new fields

Another benefit using a permanent field object is that a new field can be added or replaced with the original field. Delphi 4 allows to add five types of fields:

.DATA is used to replace the original field, for example, change the data type of the field;

.Calculate adds a "calculation field" that is given by the oncalcfields event;

.Internalcalc is similar to the "calculation field", in the client of multi-layer architecture; .lookup adds a "lookup" field;

.Aggregate adds a "total field".

The five types of five types of fields are for display only, and their data is only meaningful in the runtime, and does not affect the original data set.

To add a new field, right-click on the field editor, select the "New Field" command in the pop-up menu, pop up the "New Field" dialog, as shown in Figure 7.4

Figure 7.4 Creating a New Field Dialog

The "New Field" dialog consists of three parts: "Field Properties" packet box, "FieldType" packet box, and "lookup definition" packet box.

There are three radio buttons in the "Field Type" packet box to specify the generated type of the newly created field (not a data type), the default type is "DATA", if you select "Lookup", "Lookupdefinition" packet boxes It is activated. In addition, you can choose "Calculate" to indicate that you want to add a "calculation field".

If the data set is a TclientDataSet, the "Field Type" box is more than two radio buttons, one is "internalcalc" and the other is "aggregate".

"Field Properties" packet box is used to set the properties of the field, where the "name" box is used to enter the name of the field, and Delphi 4 generates the name of the field object in the "Component" in the "component" box according to the name field. "Type" box is used to select the data type of the field, and Delphi 4 supports the generated field of the ADT type. The "size" box is used to enter the length of the field. For the string, Bytes, and Varbytes types must enter the length, and other types of fields do not have to be.

7.4.4 Increase the field of "DATA" type

Increasing a "data" type field is mainly to replace the original field, especially the data type of the field. For example, suppose a field of a field is TsmallintField, now you want to change it into tintegerfield, because the programming is unable to change the data type of a field, you can only delete this field first, add a "data" type field. , Set its data type to Integer.

Increasing the general step of the field of a "DATA" type is:

The first step is to delete the permanent field to be replaced in the field editor to remember its name.

The second step is to click the right mouse button. In the pop-up menu, select the "New Field" command to pop up the "NewField" dialog. In the "name" box, enter the name of the field, you must be identical to the name of the field you want to replace, so you can get data from the original this field.

The third step is to set the data type and length of the new field, and must be different from the original data type. For fields of String type, only change the length of the field. It should be noted that the data type must be distinguished, the new data type must also be compatible with the original data.

The fourth step is to select "DATA" in the "Field Type" packet box, then click the OK button. At this point, a new field is created, which will replace the original one field.

7.4.5 Add the field of "Calculated" type

The "calculating field" value is given in the handle of the handle of the oncalcfields event. The general step of adding a "calculated" type field is (omitted the procedure of several public steps):

The first step is to enter the name of the field in the "Name" box, and cannot be the same as the name of the existing field.

The second step is to select the data type of the field in the "TYPE" box, specify the length of the data in the "size" box, if the type of field is TStringField, TBytesfield, or Tvarbytesfield. The third step is to select "Calculate" in the "Field Type" packet box, then click the OK button.

The fourth step is to click on the data set member on the form or data module, then establish the handle of the processed oncalcfields event, give the value of the "calculation field" in the handle, and the program is as follows:

CityStatezip.Value: = city.value ',' state.value '' zip.value;

If there is no handle to process the ONCALCFIELDS event, or the value of "calculating field" is given in the handle, the value of "calculating field" is empty.

To explain, if the data set is TclientDataSet or TQuery, you can also add a field of "interfacecalc" type. The INTERNALCALC type field is very similar to the field of the "Calculate" type. Different, for TclientDataSet, the value of the field of the "internalcalc" type is accessed with the value of the other fields.

7.4.6 Increase the field of "lookup" type

The so-called "lookup" field is its value and can only be obtained from another data.

The general step of adding a "lookup" type field is (omitted the procedure of several public steps):

The first step is to enter the name of the field in the "Name" box, and cannot be the same as the name of the existing field.

The second step is to select the data type of the field in the "TYPE" box, specify the length of the data in the "size" box, if the type of field is TStringField, TBytesfield, or Tvarbytesfield.

The third step is to select "Lookup" in the "Field Type" packet box, and the "lookupdefinition" packet box will be activated.

The fourth step is to select a dataset in the "Dataset" box of the "lookup definition" group box. This dataset cannot be the data set in "calculating field", otherwise an exception is triggered.

The fifth is to select a field as a key field in the "Key Fields" box of the "Lookup Definition" group. If you want to use several fields as a key field, you can enter these fields directly in the "Key Fields" box. The name, separated from each other. A permanent field object must be established as a field of key fields.

The sixth step is to select a field as a field as a key field in the "lookup keys" box of the "Lookup Definition" group, which is used to match the field selected by the fifth step. Note: The fields in the "DataSet" box are listed here are listed here. If you want to use a few fields as a key field, you can enter the name of these fields directly in the "Lookup Keys" box, separated from each other.

The seventh step is to select a field in the "Result Field" box of the "Lookup Definition" box. The value of the field will return to the newly added "lookup" field.

If you define both "calculation fields", the "lookup" field, the value of the "lookup" type of field will be determined first, thus, in the handle of the ONCALCFIELDS event, you can reference the "lookup" field value.

TField has a lookupCache property. For the "lookup" field, if this property is set to true, when the data set is opened for the first time, the value of the "lookup" type field is placed in the cache, you can Read the value of this field directly from the cache without having to retrieve it from another data. Obviously, this can improve the performance of the application, especially when another data set is on the remote server. However, if there is too much field specified in the "keyfields" box, it is best to set the lookupcache property to false.

Note: If you set the lookupcache property to true at the runtime, RefreshlookUplist should be called to initialize the cache.

By the way, a trick is to be introduced, you can provide data by programming the "lookup" type, which avoids data from another data set. First create a TLOOKUPLIST object instance, call the Add to create a list of data. The LookUplist property of the field is then set to specify the TLookUplist's object instance and set the lookupCache property to true. Later, the value of the "lookup" type field is read from the list.

7.4.7 Increase the field of "aggregate" type

Only the fields of the "aggregate" type can only be established in TclientDataSet, and the general steps are as follows:

The first step is to enter the name of the field in the "Name" box, and cannot be the same as the name of the existing field.

The second step is to select the data type of the field in the "Type" box, which is generally selected "aggregate".

The third step is to select "AGGREGATE" in the "Field Type" packet box, then click the OK button.

The fourth step is to click the "aggregate" type of "aggregate" type in the field editor to set an expression attribute to specify an expression.

7.4.8 Deleting permanent field objects

To delete a permanent field object, first turn on the field editor, then select the field you want to delete, press the DEL key. You can also use the mouse to right click the field you want to delete and select the "delete" command in the pop-up menu.

For a permanent field object, once you are deleted, the application can no longer access it. If you want to recover later, you can only recreate a new permanent field object, the original properties and event handles are lost.

If all permanent field objects are deleted, the dynamic field object is automatically created when the data set is opened next time.

7.5 Set the properties of the permanent field object

One advantage of using a permanent field object is that it can set its properties to create an event handle. For example, you can set the display width of the field, and it can be modified.

To set the properties of the permanent field object and create an event handle, first open the field editor, select a field in the field editor, and perform the information in the object viewer.

7.5.1 Setting the display and editing properties of fields

TField provides several attributes as follows: 00 of which is listed below due to the display and editing properties of the field:

.Alignment setting field alignment in the data control (left, home, right);

.ConstrainterrorMessage Displays the information specified by this property when the user's input violates error correction rules;

.CustomConstRaint Sets a local error correction rule;

.Currency is set to true indicating that the currency format is displayed;

.DisplayFormat Set the display format of the field in the data control;

.DisplayLabel set the column label in the database grid;

.DisplayWidth sets the display width in the data control;

.EditFormat set the display format when the field is edited;

.EDitmask must comply with the rules that the user edits data;

.Fieldkind specifies the generation type of field;

.Fieldname specifies the name of the field;

.Haasconstraints If the field already has an error correction rule, this property returns true ;. IMPORTEDCONSTRAINT Returns the error correction rules in the server-side or data dictionary;

.INDEX specifies the serial number in the data set;

.LookupDataSet Specifies another dataset to find the value of the field;

.LookupKeyFields Specifies one or more fields to match the key field;

.LookupResultfield Specifies a field that the value of this field will be copied to the lookup field;

.Maxvalue Specifies the maximum value of the field;

.Minvalue Specifies the minimum value of the field;

.Name Specifies the internal name of the permanent field object such as Table1XXH;

.Origin returns the original name in the data set;

.Precision specifies the valid bit of the field;

.Readonly is set to true indicating that the value of the field is read-only;

.Size specifies the length of the field (in characters);

.Tag gives an identifier number to each field;

.Transliterate is set to true representation will be converted according to the driver;

.Visible is set to false means that this field does not appear in the data control.

To explain, some attributes do not apply to some types of fields, for example, for a tstringfield type field, it doesn't have currency, maxValue, displayformat, etc., for a tfloatfield type field, it There is no size property.

The above properties can also be set in runtime, for example, to set the readOrthly property of the CityStatezip field of the Customers table, the program is written:

CustomerscityStatezip.readOnly: = true;

Note: The above properties cannot be accessed with the name of the field, and to access the name of the field object.

7.5.2 Setting User's Input Format

The EditMask property is only available for TStringField, TDATEFIELD, TTIMEFIELD, and TDATETIMEFIELD type fields for setting the user's input format.

To set an EditMask property, you can open the INPUT MASK Editor dialog box on the EditMask property edge in the object viewer, as shown in Figure 7.5.

Figure 7.5 SetupMask Properties dialog

You can choose an out-of-format in the "Sample Masks" box, or you can customize the format. Click the "Masks" button to select more formats.

7.5.3 Using the default display format

For TFLoatField, Tcurrencyfield, Tintegerfield, TsmallintField, TWordField, TDATEFIELD, TDATETIMEFIELD, TDATETIMEFIELD, and TTIMEFIELD types, Delphi 4 provides several routines that make these fields display in the default format. These routines are declared in the SYSUTILS unit, including:

. Formatfloat is suitable for TfloatField and Tcurrencyfield;

. FormtDatetime is suitable for TDATEFIELD, TTIMEFIELD and TDATETIMEFIELD;

. Formatcurr is suitable for Tcurrencyfield.

The so-called default format is actually set in the Control Panel. You can open the Control Panel, double-click the "Area Settings" icon, will see the format related to numbers, currencies, time, and date. For example, if the area is United States, for the TFLoatField type field, its display format is $ 1234.56.

Of course, whether it is in the design period or during the running period, you can reset the display format of the DisplayFormat property and the EditFormat property specifying the field. You can also customize the display format of the field in the handle of the ONGETTEXT event and the ONSETTEXT event. 7.5.4 Treatment Event

Just like most components, the permanent field object also has an event, and the handle of the handling event can be established during the design period. The following listings are listed in TFIELD:

.Ongence triggers this event when the value of the field changes;

.Ongettext will trigger this event when the program tries to retrieve the value of the field;

.Onettext will trigger this event when the program is trying to set the value of the field;

.OnValidate When the value of the field will be written to the recording buffer, this event will be triggered.

Where, ONGETTEXT events and ONSETTETTETTETTEXT events are used to set the field of display format, and the onchange event is used to react to data changes, such as disabling or allowing a menu item. OnValidate event checks the value of the field.

7.5.5 Call Method

The following is used to list the TFIELD:

.SsignValue is used to assign a value for fields, which will automatically perform type conversion;

.Clear seizes the value of the field as NULL;

.Getdata writes unformatted data into a buffer;

.Issvalidchar judges whether a character is legal;

.SetData assigns the unformatted data in the buffer to the field;

.FocusControl moves the input focus to the data control in which the field is located. If there are several data controls on the form display a field, FocusControl moves the input focus to the first data control that displays the field.

7.6 Attributes

If the display attributes of multiple fields are the same or substantially the same, you don't have to repeat their properties, you can set the properties of a field such as Alignment, DisplayWidth, DisplayFormat, MaxValue, MinValue, and then create an attribute set to the data dictionary. This way, other fields can introduce the property set from the data field.

To create a property set, first turn on the field editor, select a permanent field, then set the relevant properties in the object viewer. After setting, right-click on the field editor, select the "Save Attributes" command in the pop-up menu, pop up the "Save Attributes AS" dialog, as shown in Figure 7.6.

Figure 7.6 Save the property set

By default, the name of the property set is a table name plus field name, such as CustomCompany, can change this name.

You can also create a property set in SQL Explorer. The advantage of creating a property set in SQL Explorer is that you can specify the type of fields, and you can specify a data control such as TDBedit, TDBCHECKBOX, etc., which will automatically put this data control when dragging to the form based on the field of the property. Add it on the form.

After establishing the property set, other fields can introduce this property set. First, open the field editor, select the field to introduce the property set, click the right mouse button, select the "Associate Attributes" command in the pop-up menu, pop up the "Associate Attributes" dialog, as shown in Figure 7.7.

Figure 7.7 Introduced attribute set

Select a property set, click the OK button, the attribute in this property set will be applied to the field just selected, which is the so-called introduction property set.

If you don't want to use the properties in this property set, you have to open the field editor, select a field, and then right-click, select "Unassociate Attributes" in the pop-up menu.

7.7 Display, conversion, and access fields

The data control can automatically display the value of the field. In the case of allowing editing, the user can modify the value of the field in the data control, and write the modified value back to the data set, which does not need to write code. The application can access the value of the field by tfield's Value property, for example, the following line code displays the value of the CustomersCompany field in the edit box edit1:

Edit1.Text: = CustomersCompany.Value;

In theory, the Value property can access the value of the fields of any data type. However, many data controls can only pass the value of the string type, so it is necessary to convert the Value property.

Fortunately, there are numerous conversion functions in TField, which can access a field value in a specific data type, for example, Asstring can access the digital or Boolean fields to the string type.

Asvariant is suitable for all data types. To explain, the conversion is not always successful, for example, asDateTime can convert a string to a date, but the string itself must recognize the datetime format, and cannot be a normal string. In some cases, although the conversion can be carried out, it is possible to lose accuracy.

Values ​​to access fields with the Value property require the name of the field object in advance, and in many cases, often do not know the name of the field object, but the name of the field is known. Therefore, the application often accesses the value of the field through the serial number or name of the field.

The data set components have a fields attribute. This is an array. Each element represents a field so that you can access the value of a field according to the serial number, and the serial number starts from 0. For example, the following code displays the 7th field of the Customers table into the edit box Edit1:

Edit1.text: = CustTable.fields [6] .sstring;

The following code sets the value of the seventh field to the edit box edit1:

Customers.edit;

Customers.fields [6] .sstring: = Edit1.Text;

Customers.post;

However, the value of accessing the field according to the serial number is not very safe, because if the serial number is recorded, it will not be able to get the correct result. Therefore, we recommend to access the value of the field by field name, which is used to use the fieldbyname function.

To call the fieldbyName function, you have to pass the name of the field as the parameter to the FieldByname function. For example, the following code displays the Customers table's CUSTNO field to the edit box Edit2:

Edit2.text: = Customers.fieldbyName ('Custno'). Asstring;

The following code sets the value of the CustNo field to the contents of the edit box Edit2:

Customers.edit;

Customers.fieldbyName ('Custno'). Asstring: = Edit2.Text;

Customers.post;

In applications for multi-layer architecture, the client program is possible to make an error when applying the application server to the application server. This time you can check the current value of the field with the CurValue property, because the value of the field is possible to modify, especially in more In the user's environment, therefore, the current value of the field is very necessary.

7.8 field level error correction

The so-called field-level error correction is to define some rules or conditions in advance. The value of the field must meet these rules or conditions, otherwise it is wrong. Field objects can borrow the SQL server error correction, or create a custom error correction.

Most SQL servers define error correction, while client programs can borrow server error correction. TField's importedConstRAINT property can return the server's error correction rule, which is actually a string, its syntax is similar to the SQL statement, for example:

Value> 0 and value <100

If the server's error correction still does not meet the requirements, you can define the error correction rules yourself with the CustomConstRAINT property. This property is different from the ImportedConstraint property. If the server-side error correction rule changes, the importedConstRAINT attribute will also change, and the custom error correction rule is constant.

Custom error correction is only available for this application and only checks the data entered by the user, and the data that is sent to the application server or from the application server is not powerful.

To customize the error correction rule, use the CustomConstraint property to specify the error correction rule, and set the constrainterrorMessage property to specify a message, which will display this information when the value of the field violates the error correction rules. Custom error correction rules are similar to the WHERE section of the SQL statement, for example: x> 0 and x <100 where X is just a symbol, used to reference the value of the field, the symbol itself is irrelevant, as long as the error correction expression It will be held in consensus.

7.9 Object Field of Oracle 8

Delphi 4 is the best tool for playing Oracle 8 objects that support four newest fields, including ADT (Abstract Data Types), arrays, data sets (nested tables), references, and their corresponding field types are as follows Column:

.Tadtfield represents an ADT field;

.Tarayfield represents an array field;

.Tdatasetfield represents a dataset field;

.Treferencefield represents a reference field and pointing to another ADT field.

The four types of fields are collectively referred to as object fields, and one of their basic features is included or referenced. If a dataset contains an object field, you will automatically create a permanent field object for the subfields contained or referenced by these object fields as long as the permanent field objects of the object field are created during the design period. At the same time, the ObjectView property of the data set component is automatically set to TRUE. In the case where the ObjectView property is set to TRUE, the fields in the data set are stored in a tree according to their inheritance, rather than linearly stored in the original serial number.

Some of the properties and methods of the object field are listed below, which is used to manipulate the subfields that it contains or references:

.Fields uses this property to access the subfields containing or referenced by the object field;

. ObjectType returns the type of object;

.Fieldcount returns the number of subfields included or referenced by the object field;

.Fieldvalues ​​Use this property to access the value of the subfield containing or referenced by this property.

7.9.1 ADT field

ADT is a user-defined field type, a bit similar to the structure, which can contain most types of fields, including arrays, references, and nested tables.

There are several ways to access an ADT type, it is best to create a permanent field object. Suppose an ADT type field called Address, which contains four subfields, STREET, CITY, STATE, ZIP, respectively. If you create a permanent field object for the Address field, you will see the following permanent field objects:

Customeraddress: tadtfield;

CustomerAddrstreet: tstringfield;

Customeraddrcity: tstringfield;

CustomerAddrstate: tstringfield;

CustomeraDdrzip: Tstringfield;

In this way, the subfield of the object field can be used as a normal field, for example: cityedit.text: = CustomerAddrcity.Asstring;

However, the above code assumes that the permanent field object of the object field has been created. If you do not create a permanent field, use the following method to access the subfield field of the object field:

CityEdit.Text: = Customer.fieldByname ('address.city'). Asstring;

You can access the value of the subfield with Tadfield's fieldVALUES property, for example:

CityEdit.Text: = tadtfield (Customer.fieldByname ('address')) [1];

Since the FieldValues ​​property is the default attribute of TOBJECTFIELD, the above code can be changed to:

CityEdit.text: = tadtfield (Customer.fieldByname ('address')). FieldValues ​​[1];

The above code can also be used to use the fields attribute, such as: cityEdit.text: = tadtfield ('address'). Fields [1] .sstring;

Even can write this: cityedit.text: = tadtfield ('address'). Fields.fieldbyName ('city'). Asstring;

It can be seen that the subfield for accessing the object field is the easiest through the permanent field object. In addition, the ADT field can also be accessed through the fieldVale attribute of the data set member, for example:

Customer.edit; Customer ['address.city']: = cityEdit.Text; Customer.post;

The following line code displays the value of the CITY subfield of the Address field to the edit box at CityEdit:

CityEdit.text: = Customer ['address.city'];

7.9.2 array fields

The so-called array field, its concept is similar to an array, and the type of subfield it contains is the same, and the type of subfield included in the ADT field can be different. The subfield of the array field can be a normal field, or the ADT field, but it is not another array field.

There are several ways to access the array fields in the data set, and below is a typical way of access:

VAR

ORDERDATES: TARRAYFIELD;

I: integer;

Begin

For i: = 0 to ORDERDATES.SIZE - 1 DO

Begin

IF OrderDates.fields [i] .is null the Break;

ORDERDATELISTBOX.ITEMS.ADD (ORDERDATES [I]);

END;

END;

Suppose there is a array field called Telnos_Array, which has 6 string type subfields. When you create a permanent field object for the Telnos_Array field, the permanent field object of 6 subfields is created at the same time:

Customertelnos_Array: Tarrayfield;

Customertelnos_Array0: Tstringfield;

Customertelnos_Array1: Tstringfield;

Customertelnos_Array2: Tstringfield;

Customertelnos_Array3: Tstringfield; Customertelnos_Array4: TstringField;

Customertelnos_Array5: Tstringfield;

Below this line of code accesses the first subfield:

Teledit.text: = Customertelnos_Array0.Asstring;

If you do not create a permanent field object for the array field, you can only access the value of the subfield through the fieldVALUES properties, but you must set the ObjectView property of the data set component to TRUE. E.g:

Teledit.text: = tarrayfield (TABLE1.FIELDBYNAME ('Telnos_Array')). FieldValues ​​[1];

Since the FieldValues ​​property is the default attribute of TOBJECTFIELD, the above code can be changed to:

TEledit.text: = tarrayfield (Table1.fieldByname ('Telnos_Array')) [1];

The above code can also be written: Teledit.Text: = tarrayfield (Customer.fieldByname ('telnos_array'). Fields [1] .sstring;

7.9.3 Dataset field

Through the data set field, you can easily maintain a pair of relationships between the tables and the table. The NestedDataSet property through TDataSetfield can access the sub-data set nested by the Dataset field.

In order to support the data set field, the TDBGRID component of Delphi 4 has been rewritten. Anyone who displays the data set field has a omitted button. The user clicks this omitial number will open a new window. This window also displays nested with a TDBGRID component. Data of the table. Of course, to open this window, you can call TDBGRID's showPopupeditor function, for example:

DBGRID1.SHOWPOPEDITOR (DBGrid1.columns [7]);

To access data in the nested table, it is best to create a permanent field object for the Data Set field, and then specify this field with the TNESTETETABLE or TCLIELD attribute of TnestedTable or TclientDataSet.

7.9.4 Reference Field

The reference field is expressed and stored a pointer, pointing to another ADT object, this ADT object is usually a record in another data set. The data in the referenced ADT object can be accessed like a nested table or can be accessed through the TreferenceField's fields attribute.

Like the data set field, in the TDBGRID component, all the columns that display the reference field have a omitted button, the user clicks this omitial button will open a new window, this window also uses the raster to display the referenced ADT object. data. To open this window, you can also call the TDBGRID's showPopupeditor function, for example:

DBGRID1.SHOWPOPEDITOR (DBGrid1.columns [7]);

To access the data in the referenced ADT object, first create a permanent field object for the reference field, then use the TNESTETABLE or TCLIELD attribute to specify this field.

Suppose a reference field object is called CustomerRefCity, the following code accesses a field in the ADT object it references, the effect of the two lines of code is the same.

CityEdit.Text: = Customerrefcity.fields [1] .sstring;

CityEdit.Text: = CustomerRefcity.nestedDataSet.fields [1] .Asstring; assign a reference field to the reference field, you need to use the SQL SELECT statement, the program 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 the ADT field and array fields

The ADT field and array fields have a common feature that they all contain subfields. Delphi 4 has been rewritten on some data controls, like TDBEDIT, TDBGRID supports ADT fields and array fields.

For data controls with DataField properties, they displays the values ​​of their subfields when they display the value of the ADT field and array fields.

TDBGRID How to display the value of the ADT field and the array field depends on how the ObjectView attribute of the data set component is set. If the ObjectView property is set to False, each subfield is set in the column. If the ObjectView property is set to true, a bar displays the ADT field and an array field will appear, click this arrow to extend the raster to display the value of all subfields.

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

New Post(0)