運行時:
//先在窗體上放置: DBGrid1、DataSource1、ClIEntDataSet1 並關聯, 然後:
procedure TForm1.FormCreate(Sender: TObject);
begin
//ClientDataSet1.ObjectVIEw := True; { 這是默認的 }
{ 先定義兩個基本字段 }
with ClientDataSet1.FieldDefs.AddFIEldDef do begin
Name := 'ID';
DataType := ftInteger;
end;
with ClientDataSet1.FieldDefs.AddFIEldDef do begin
Name := 'Name';
DataType := ftString;
Size := 7;
end;
{ 定義 ftADT 字段, 聯系方式(Contact)中包括: Tel、QQ、Email }
with ClientDataSet1.FieldDefs.AddFIEldDef do begin
Name := 'Contact';
DataType := ftADT;
Size := 3;
ChildDefs.Add('Tel', ftString, 12);
ChildDefs.Add('QQ', ftString, 9);
ChildDefs.Add('Email', ftString, 15);
end;
ClIEntDataSet1.CreateDataSet;
{ 添加記錄, 這種最方便 }
ClIEntDataSet1.AppendRecord([1, '張三', VarArrayOf(['9111111', '12222', 'aaa@bbb'])]);
{ 下面的其他添加方法也可用於訪問 }
ClIEntDataSet1.Append;
ClIEntDataSet1['ID'] := 2;
ClIEntDataSet1['Name'] := '李四';
ClIEntDataSet1['Contact.Tel'] := '9222222';
ClIEntDataSet1['Contact.QQ'] := '13333';
ClIEntDataSet1['Contact.Email'] := 'ccc@ddd';
ClIEntDataSet1.Post;
ClIEntDataSet1.Append;
ClientDataSet1.FIElds[0].AsInteger := 3;
ClientDataSet1.FIElds[1].AsString := '王五';
TADTField(ClientDataSet1.FIElds[2])[0] := '9333333';
TADTField(ClientDataSet1.FIElds[2])[1] := '14444';
TADTField(ClientDataSet1.FIElds[2])[2] := 'eee@fff';
ClIEntDataSet1.Post;
ClIEntDataSet1.Append;
ClientDataSet1.FIEldByName('ID').Value := 4;
ClientDataSet1.FIEldByName('Name').Value := '孫六';
TADTField(ClientDataSet1.FieldByName('Contact')).FIEldValues[0] := '9444444';
TADTField(ClientDataSet1.FieldByName('Contact')).FIEldValues[1] := '15555';
TADTField(ClientDataSet1.FieldByName('Contact')).FIEldValues[2] := 'ggg@hhh';
ClIEntDataSet1.Post;
end;
設計時:
查看原圖(大圖)
這種復雜點的數據可以保存為 cds, 但保存不了 xml, 且 XMLData 也生成不了.