TClientDataSet中关于TField、TFieldDef动态创立字段的应用

//使用 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; 


你可能感兴趣的:(client)