Multi-layer database development seven: Field objects Select BLOG in Cyrtsoft

xiaoxiao2021-03-06  65

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 ADT fields. ADT Abstract Data Type is an abbreviation; .TAggregateFieldTClientDataSet automatically maintained total field; .TArrayField array fields; automatic growth field .TAutoIncFieldParadox the table, ranging 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 field, value range from 5.0e-324 to 1.7e308; .tdatasetfield represents the field of nested tables; .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 integer field, value range from -2, 147, 483, 648 to 2, 147, 483, 647; .tlargeintfield long integer field, value range from -263 to 263; .tmemofield Remark field, theoretical length 2GB; .tnumericfield real number, value range from 3.4 E-4932 to 1.1e4932; .treferencefield A reference other Datasefield Pointer; .TSMallintField integer field, value range from -32, 768 to 32, 768; .TStringfield string field, maximum byte number 8192, including null characters; .tttimefield 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 is like the Delphi 4 data access components such as TTABLE, TQUERY, TField is also non-visual, even can't see it directly in 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 look at TFLoatField as an example to see how to manipulate the display attribute of the field: .alignment is used to specify the alignment of the data, such as the left alignment, home, right right ;.displaywidth is used to specify the display width of the data (number of characters) Unit) ;. 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, Delphi 4 automatically generates a dynamic field object for each field in the data set when a data set is placed on a data module. 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 "Calculate Fields", "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 Objects Use a permanent field object to replace the dynamic field object to set its properties in the design period. In addition, the permanent field object also has the following advantages: You can select some fields; you 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, you should 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 step of creating a permanent field object is: The first step is to place a data set component such as TTABLE to 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 Field Editor consists of a list of tablets, 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 a field to select one or several fields, and then 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 List the name navigation button of the permanent field object is ash, possibly 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 creating permanent field object can be found in the cell file declaring the field object, the following example: TypeTForm1 = Class (TForm) Table1: TTable; Button1: TButton; Table1XXH1: TStringField; Table1XXH2: TBytesField; Procedure Button1Click (Sender: TObject ); Private {private declarations} end; 7.4.2 Adjusting the order of permanent fields Permanent field objects 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 Field Use another benefit of using a permanent field object to add a new field or replace the original field. Delphi 4 allows for adding 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", the value of this field is given by the oncalcfields; .internalCalc Similar In 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 7.4 Create a new field dialog box " 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 Increasing the "DATA" field Add a "DATA" 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. The general step of adding a "data" type field is: The first step is to delete the permanent field to be replaced in the field editor, 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 Increasing the "Calculate" type field "Calculate 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 component on the form or data module, then establish the handle of the ONCALCFIELDS event, give the "calculation field" value in the handle, the program example is as follows: CityStatezip.value: = city.value ',' State.value '' zip.value; if there is no handle that processes the oncalcfields event, or the value of the "calculation 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 Increasing the field of the "lookup" type, the field of "lookup" type, is its value and can only be obtained from another data. The general step of adding a "lookup" type field is (omitted the first few 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 Increasing the "aggregate" type field can only add the "aggregate" type field to the data set created in TclientDataSet. The general steps are as follows: The first step is to enter the name of the field in the "Name" box, cannot be The name of the field is the same. 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 a permanent field object To delete a permanent field object, first open 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 Setting the properties of the permanent field object Use a advantage of the permanent field object to set its properties in the design period, establish 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 Settings Field Display and Editing Properties TField provides several properties to set the display and editing properties of the field, these properties are listed below: .alignment Settings fields in the alignment in the data control (left, home, right); .ConstrainterrorMessage Displays the information specified by this property when the user's input violates the error correction rules; .customConStraint sets a local error correction rule; .currency is set to true indicates that the currency format is displayed ;.displayFormat set field in the data control Display format ;.displayLabel Set the column label in the database grid; .displayWidth Set the display width in the data control; .EditFormat set the field in the display format when editing; .editMask Set the rules that the user must comply when editing data .Fieldkind specifies the generation type of the field; .fieldName Specifies the name of the field; .hasConstraints If the field already has error correction rules; .importedConstRAINT returns the error correction rule in the server or data dictionary; .index specified field The serial number in the data set; the .lookupDataSet specifies another dataset to find the value of the field; .lookupKeyFields Specify one or more fields to match the key field; .lookupResultfield specifies a field, the value of this field will be copied Give 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 .precision specified field .Readonly is set to true indicates that the value of the field is read-only ;.size specifies the length of the field (in characters) ;. TAG gives each field object one identifier number ;.Transliterate is set to true indicating that the driver is performed according to the driver Conversion; .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 ReadOnly property of the Customers table, the program written: CustomersCityStatezip.readonly: = true; Note: You cannot use the name of the field to access the above properties, and use field objects Name to access. 7.5.2 Setting the user's input format editmask property only for the field of TStringField, TDATEFIELD, TTIMEFIELDFIELD, TDATETIMEFIELD type, used to set 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 Setting the EditMask Properties dialog box can be used in the "Sample Masks" box to select an outgoing format or custom format. Click the "Masks" button to select more formats. 7.5.3 Using the default display format For TFLoatField, TcurrenField, TintegerField, Tsmallintfield, TintegerField, TDateField, TDATIMEFIELD, and TTIMEFIELD type fields, 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; .formatDateTime 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 Events 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 is listed below: .onChange triggers the event when the value of the field changes; .ongetText When the program tries to retrieve the value of the field, the event will trigger the event; .onSettext Triggered this event when the program is trying to set the value of the field .OnValidate will trigger this event when the value of the field will be written to the recording buffer. 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: .AssignValue is used to assign a field, which will automatically perform type conversion; .Clear seizes the value of the field as null; .Getdata Write unformatted data In a buffer; .isvalidchar judges whether a character is legal; .SetData assigns an unformatted data in the buffer to the field; .focusControl moves the input focus to the data control in which the input focus 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 Attribute Set 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 a field of properties such as Alignment, DisplayWidth, DisplayFormat, MaxValue, MinValue, and then create a property closure to the data dictionary in. 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, 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 Introduction Attribute Set Select a property set, click the OK button, the attribute set in this property will be applied to the field just selected, this 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 value of the field 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, all all Don't write code. The application can access the value of the field through the value of TField, for example, the following line code display the value of the CustomersCompany field in the edit box Edit1: edit1.text: = CustomersCompany.Value; From theory, the Value property can access any The value of the field of the 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 seventh 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 edit box Edit1 Content: Customers.edit; Customers.Fields [6] .sstring: = Edit1.Text; Customers.post; However, the value of accessing the field is not very safe, because if you remember the wrong serial number, you will not be correct. the result of. 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 CustNo field of the Customers table into the edit box edit2: edit2.text: = Customers.fieldbyName ('Custno'). Asstring; the following code sets the value of the CustNo field to edit box edit2 : Customers.EDIt; Customers.FieldByname ('Custno'). Asstring: = Edit2.text; Customers.post; In the multi-layer architecture application, the client program is possible to update the data to the application server, this time You can use the CurValue property to check the current value of the field because the value of the field is possible to modify, especially in the multi-user environment, so knowing the current value is necessary. 7.8 Field-Level Correction The so-called field-level error correction is that some rules or conditions are defined in advance, and the value of the field must meet these rules or conditions, otherwise it is considered an error. 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, which is similar to the SQL statement, for example: value> 0 and value <100 If the server's error correction does not meet the requirements, you can use the CustomConstraint property. Define your error correction rules yourself. 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 Oracle 8 Object Field Delphi 4 is the best tool for playing Oracle 8 object association, which supports four newest fields, including ADT (abstract data types), arrays, data sets (nested tables), reference, The corresponding field type is listed below: .tadtfield represents an ADT field; .taRrayField represents an array field ;.tdataSetField represents a dataset field; .treferenceField represents a reference field, 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 objects and methods of the object field are listed below, which is used to manipulate the subfields that it contains or reference: .fields can access the subfields containing or referenced by this property; .ObjectType returns the type of object; FieldCount returns the number of subfields included or referenced by the object field; .fieldValues ​​can 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 persistent field object for the Address field, you will see the following persistent field objects: CustomerAddress: TADTField; CustomerAddrStreet: TStringField; CustomerAddrCity: TStringField; CustomerAddrState: TStringField; CustomerAddrZip: TStringField; this way, you can put sub-field object fields when For a normal field, for example: cityEdDrcity.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 object field. Subfield: cityedit.text: = Customer.fieldByname ('address.city'). Asstring; You can use tadtfield's FieldValues ​​property to access the value of the subfield, for example: cityEdit.text: = tadtfield (Customer.fieldbyName ('address') ) [1]; Because the fieldValues ​​property is the default attribute of TOBJECTFIELD, the above code can be changed to: cityedit.text: = tadtfield ('address')). FieldValues ​​[1]; The above code can also be changed to Use the fields attribute, for example: cityEdit.text: = tadtfield ('address'). Fields [1] .sstring; can even write: cityedit.text: = tadtfield (Customer.fieldbyName ('address') .Fields.fieldbyname ('city'). Asstring; It can be seen that the subfield for accessing the object field by permanent field object is the easiest.

In addition, the FIELDVALUES attribute of the data set member can also access the ADT field, for example: Customer.edit; Customer ['address.city']: = cityEdit.text; customer.post; below this line code puts the city subfield of the Address field The value is displayed in the edit box cityEdit: cityEdit.Text: = Customer ['address.city']; 7.9.2 Array Field so-called array field, its concept is similar to array, the type of subfields it contains is the same, 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. Below is a typical way of access: varorderdates: tarrayfield; i: integer; beginfor i: = 0 to orderdates.size - 1 dobeginif OrderDates.fields [i] .is null Then Break; ORDERDATELISTBOX.Items.Add (ORDERDATES [I]); END; END; assumes that there is an array field called Telnos_Array, which has 6 string type subfields. When creating permanent field object as TelNos_Array field will be created 6 sub-fields of the permanent field objects: CustomerTelNos_Array: TArrayField; CustomerTelNos_Array0: TStringField; CustomerTelNos_Array1: TStringField; CustomerTelNos_Array2: TStringField; CustomerTelNos_Array3: TStringField; CustomerTelNos_Array4: TStringField; CustomerTelNos_Array5: TStringField; below This line code is accessing the first subfield: Teledit.Text: = Customertelnos_Array0.Asstring; If you do not create a permanent field object for array fields, you can only access the value of the subfield by fieldvalues ​​property, but you must put the data set components. The ObjectView property is set to TRUE. For example: teledit.text: = tarrayfield ('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 this: TEledit.text: = tarrayfield ('telnos_array'). Fields [1] .sstring; 7.9.3 Dataset field via data set Fields, you can easily maintain a pair of relationships between tables and tables. You can access subchannel sets nested by TDataSetfield's NESTEDDataSet attribute. In order to support data set fields, Delphi 4's TDBGRID components have been rewritten. Anything that displays the data set field has a omitted button, the user clicks this omitted number will open a new window, this window also displays the data of the nested table with a TDBGRID component.

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

New Post(0)