zoukankan      html  css  js  c++  java
  • TClientDataSet[21]: 使用 ADT(Abstract Data Type) 字段


    运行时:



    //先在窗体上放置: 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 也生成不了.

  • 相关阅读:
    转载一个好用的div弹出层插件
    asp.net 母版页使用方法
    visual studio 代码排版组合键
    模仿米折网商品图片自动翻页效果
    BinaryWriter 、BinaryReader在读写
    Java 8 Lambda 表达式
    IBeacon协议分析
    Centos配置jdk和tomcat环境
    apidoc 生成Restful web Api文档
    数组和链表的区别
  • 原文地址:https://www.cnblogs.com/del/p/1664168.html
Copyright © 2011-2022 走看看