[ Pobierz całość w formacie PDF ]
.This is because all field data types thatsupport one of those properties always support the others as well.Note also that the AsVariant property can translate among all data types.For anydata types not listed above, AsVariant is also available (and is, in fact, the onlyoption).When in doubt, use AsVariant.In some cases, conversions are not always possible.For example, AsDateTime can beused to convert a string to a date, time, or datetime format only if the string value isin a recognizable datetime format.A failed conversion attempt raises an exception.In some other cases, conversion is possible, but the results of the conversion are notalways intuitive.For example, what does it mean to convert a TDateTimeField valueinto a float format? AsFloat converts the date portion of the field to the number of19-18 Dev el oper s Gui deDi s p l a y i n g , c o n v e r t i n g , a n d a c c e s s i n g f i e l d v a l u e sdays since 12/31/1899, and it converts the time portion of the field to a fraction of 24hours.Table 19.7 lists permissible conversions that produce special results:Table 19.7 Special conversion resultsConversion ResultString to Boolean Converts True, False, Yes, and No to Boolean.Other values raiseexceptions.Float to Integer Rounds float value to nearest integer value.DateTime or Converts date to number of days since 12/31/1899, time to a fraction of 24SQLTimeStamp hours.to FloatBoolean to String Converts any Boolean value to True or False.In other cases, conversions are not possible at all.In these cases, attempting aconversion also raises an exception.Conversion always occurs before an assignment is made.For example, the followingstatement converts the value of CustomersCustNo to a string and assigns the string tothe text of an edit control:Edit1.Text := CustomersCustNo.AsString;Conversely, the next statement assigns the text of an edit control to theCustomersCustNo field as an integer:MyTableMyField.AsInteger := StrToInt(Edit1.Text);Accessing field values with the default dataset propertyThe most general method for accessing a field s value is to use Variants with theFieldValues property.For example, the following statement puts the value of an editbox into the CustNo field in the Customers table:Customers.FieldValues['CustNo'] := Edit2.Text;Because the FieldValues property is of type Variant, it automatically converts otherdatatypes into a Variant value.For more information about Variants, see the online help.Accessing field values with a dataset s Fields propertyYou can access the value of a field with the Fields property of the dataset componentto which the field belongs.Fields maintains an indexed list of all the fields in thedataset.Accessing field values with the Fields property is useful when you need toiterate over a number of columns, or if your application works with tables that arenot available to you at design time.To use the Fields property you must know the order of and data types of fields in thedataset.You use an ordinal number to specify the field to access.The first field in adataset is numbered 0.Field values must be converted as appropriate using eachWor k i ng wi t h f i el d c omponent s 19-19Se t t i n g a d e f a u l t v a l u e f o r a f i e l dfield component s conversion properties.For more information about fieldcomponent conversion properties, see Converting field values on page 19-17.For example, the following statement assigns the current value of the seventh column(Country) in the Customers table to an edit control:Edit1.Text := CustTable.Fields[6].AsString;Conversely, you can assign a value to a field by setting the Fields property of thedataset to the desired field.For example:beginCustomers.Edit;Customers.Fields[6].AsString := Edit1.Text;Customers.Post;end;Accessing field values with a dataset s FieldByName methodYou can also access the value of a field with a dataset s FieldByName method.Thismethod is useful when you know the name of the field you want to access, but do nothave access to the underlying table at design time.To use FieldByName, you must know the dataset and name of the field you want toaccess.You pass the field s name as an argument to the method.To access or changethe field s value, convert the result with the appropriate field component conversionproperty, such as AsString or AsInteger.For example, the following statement assignsthe value of the CustNo field in the Customers dataset to an edit control:Edit2.Text := Customers.FieldByName('CustNo').AsString;Conversely, you can assign a value to a field:beginCustomers.Edit;Customers.FieldByName('CustNo').AsString := Edit2.Text;Customers.Post;end;Setting a default value for a fieldYou can specify how a default value for a field in a client dataset or a BDE-enableddataset should be calculated at runtime using the DefaultExpression property.DefaultExpression can be any valid SQL value expression that does not refer to fieldvalues.If the expression contains literals other than numeric values, they mustappear in quotes.For example, a default value of noon for a time field would be 12:00:00including the quotes around the literal value.Note If the underlying database table defines a default value for the field, the default youspecify in DefaultExpression takes precedence.That is because DefaultExpression isapplied when the dataset posts the record containing the field, before the editedrecord is applied to the database server.19-20 Dev el oper s Gui deWo r k i n g wi t h c o n s t r a i n t sWorking with constraintsField components in client datasets or BDE-enabled datasets can use SQL serverconstraints
[ Pobierz całość w formacie PDF ]