TClientDataSet[7]: 辨析 Field、FieldDef、Fields、FieldDefs、FieldList、FieldDefList


TField TFieldDef TFields TFieldDefs TFieldList、TFieldDefList
Assign
AssignValue
Clear
FocusControl
GetData
GetParentComponent
HasParent
IsBlob
IsValidChar
RefreshLookupList
SetData
SetFieldType
SetParentComponent
Validate

AsBCD
AsBoolean
AsCurrency
AsDateTime
AsSQLTimeStamp
AsSQLTimeStampOffset
AsSingle
AsFloat
AsExtended
AsInteger
AsLargeInt
AsString
AsWideString
AsAnsiString
AsBytes
AsVariant
AttributeSet
Calculated
CanModify
CurValue
DataSet
DataSize
DataType
DisplayName
DisplayText
EditMask
EditMaskPtr
FieldNo
FullName
IsIndexField
IsNull
Lookup
LookupList
NewValue
Offset
OldValue
ParentField
Size
Text
ValidChars
Value
Alignment
AutoGenerateValue
CustomConstraint
ConstraintErrorMessage
DefaultExpression
DisplayLabel
DisplayWidth
FieldKind
FieldName
HasConstraints
Index
ImportedConstraint
LookupDataSet
LookupKeyFields
LookupResultField
KeyFields
LookupCache
Origin
ProviderFlags
ReadOnly
Required
Visible

OnChange
OnGetText
OnSetText
OnValidate
AddChild
Assign
CreateField
HasChildDefs

FieldClass
FieldNo
InternalCalcField
ParentDef
Required
Attributes
ChildDefs
DataType
Precision
Size
Name
GetNamePath
Collection
ID
Index
DisplayName
Add
CheckFieldName
CheckFieldNames
Clear
FindField
FieldByName
FieldByNumber
GetEnumerator
GetFieldNames
IndexOf
Remove
Count
DataSet
Fields
AddFieldDef
Find
Update
Add
GetItemNames
IndexOf
Owner
Assign
BeginUpdate
Clear
Delete
EndUpdate
FindItemID
GetEnumerator
GetNamePath
Insert

HiddenFields
Items
ParentDef
DataSet
Updated
Capacity
Count
ItemClass
FieldByName
Find
Fields(FieldDefs)
Update
DataSet
Add
AddObject
Clear
Delete
Exchange
IndexOf
Insert
InsertObject
Sort
CustomSort
Append
AddStrings
Assign
BeginUpdate
EndUpdate
Equals
GetEnumerator
GetText
IndexOfName
IndexOfObject
LoadFromFile
LoadFromStream
Move
SaveToFile
SaveToStream
SetText

Duplicates
Sorted
CaseSensitive
Capacity
CommaText
Count
Delimiter
DelimitedText
LineBreak
Names
Objects
QuoteChar
Values
ValueFromIndex
NameValueSeparator
StrictDelimiter
Strings
Text
StringsAdapter

OnChange
OnChanging
OwnsObjects


发现这些东西早在 Delphi 3 的时候就比较成熟了, 至今也基本没有变化; 我暂时对它们的理解是:

1、Fields 是 Field 的集合, 它们主要用于运行时对字段元数据和字段值的访问.

2、FieldDefs 是 FieldDef 的集合, 它们主要用于构建数据集(表)和对字段元数据的访问.

3、FieldList 和 FieldDefList 分别是访问 Field 和 FieldDef 的快捷列表; 主要使用其 FieldByName、Find 方法和默认的数组属性访问数据; 它们是只读的.

4、通过 Fields、FieldList、Field 可以得到更多信息, 但必须是在数据集打开的情况下;
通过 FieldDefs、FieldDefList、FieldDef 只能获取定义时的信息, 但即使在数据集关闭时也能使用.

5、顾名思义 FieldDef 是用于定义表的, 但通过 Field 也可以定义表;
用 FieldDef 定义表很方便, 用 Field 可以定义一些更复杂的表;
每个 FieldDef 都会对应一个 Field, 但一个 Field 不一定有 FieldDef 对应;
程序运行后 FieldDef 不能再改变, 而 Field 与 Fields 则可以动态改变或增减.

设计时两者是结合使用的.
下面是通过三种方法动态建表的代码:
//使用 TFieldDef 建表:
begin
  with ClientDataSet1.FieldDefs do
  begin
    Add('Name', ftString, 12, True); { True 表示是必填字段 }
    Add('Age', ftInteger);
  end;
  ClientDataSet1.CreateDataSet;
end;

//使用 TField(这里是用其子类)建表:
begin
  with TStringField.Create(Self) do
  begin
    FieldName := 'Name';
    Size := 12;
    Required := True; { 必填字段 }
    DataSet := ClientDataSet1;
  end;
  with TIntegerField.Create(Self) do
  begin
    FieldName := 'Age';
    DataSet := ClientDataSet1;
  end;
  ClientDataSet1.CreateDataSet;
end;

//混合使用(这好像就是设计时的情形):
var
  F: TIntegerField;
begin
  with ClientDataSet1.FieldDefs.AddFieldDef do
  begin
    Name := 'Name';
    DataType := ftString;
    Size := 12;
    Required := True;
    CreateField(ClientDataSet1);
  end;
  with ClientDataSet1.FieldDefs.AddFieldDef do
  begin
    Name := 'Age';
    DataType := ftInteger;
    { 指定最大值和最小值 }
    F := CreateField(ClientDataSet1) as TIntegerField;
    F.MinValue := 0;
    F.MaxValue := 150;
  end;
  ClientDataSet1.CreateDataSet;
end;

 
 
 
 
 

 

 
  

字段类型列表:
TStringField          { ftString         ; String }
TWideStringField      { ftWideString     ; WideString }
TNumericField         { }
TIntegerField         { ftInteger        ; Integer }
TLongWordField        { ftLongWord       ; LongWord }
TSmallintField        { ftSmallint       ; Smallint }
TShortintField        { ftShortint       ; Shortint }
TByteField            { ftByte           ; Byte }
TLargeintField        { ftLargeint       ; Int64 }
TWordField            { ftWord           ; Word }
TAutoIncField         { ftAutoInc        ; Integer }
TUnsignedAutoIncField { ftAutoInc        ; Cardinal }
TFloatField           { ftFloat          ; Double }
TSingleField          { ftSingle         ; Single }
TCurrencyField        { ftCurrency       ; Currency }
TExtendedField        { ftExtended       ; Extended }
TBooleanField         { ftBoolean        ; WordBool }
TDateTimeField        { ftDateTime       ; DateTime }
TSQLTimeStampField    { ftTimeStamp      ; TSQLTimeStamp }
TSQLTimeStampField    { ftTimeStampOffset; TSQLTimeStampOffset }
TDateField            { ftDate           ; Integer }
TTimeField            { ftTime           ; Integer }
TBinaryField          { }
TBytesField           { ftBytes          ; * }
TVarBytesField        { ftVarBytes       ; * }
TBCDField             { ftBCD            ; * }
TFMTBCDField          { ftFMTBCD         ; * }
TBlobField            { ftBlob           ; * }
TMemoField            { ftMemo           ; * }
TWideMemoField        { ftWideMemo       ; * }
TGraphicField         { ftGraphic        ; * }
TObjectField          { }
TADTField             { ftADT            ; * }
TArrayField           { ftArray          ; * }
TDataSetField         { ftDataSet        ; TDataSet }
TReferenceField       { ftReference      ; * }
TVariantField         { ftVariant        ; OleVariant }
TInterfaceField       { ftInterface      ; IUnknown }
TIDispatchField       { ftIDispatch      ; IDispatch }
TGuidField            { ftGuid           ; TGUID }
TAggregateField       { ftUnknown        ; * }

 
 
 
 
 

 

 
  

你可能感兴趣的:(TClientDataSet[7]: 辨析 Field、FieldDef、Fields、FieldDefs、FieldList、FieldDefList)