随笔 - 2146  文章 - 97 评论 - 11767 trackbacks - 253


TClientDataSet.Data    { 数据的二进制格式的描述 }
TClientDataSet.XMLData { 数据的 XML 格式的描述 }


//先在窗体上放置 ClientDataSet1、Memo1 和三个按钮

uses XMLDoc, IOUtils; { 分别提供 FormatXMLData 和 TFile.ReadAllText 函数 }

{ 设定当前目录 }
procedure TForm1.FormCreate(Sender: TObject);
begin
  ChDir(GetEnvironmentVariable('COMMONPROGRAMFILES') + '\CodeGear Shared\Data\');
end;

{ 读取 XMLData 测试 }
procedure TForm1.Button1Click(Sender: TObject);
begin
  ClientDataSet1.LoadFromFile('employee.cds');
  Memo1.Text := FormatXMLData(ClientDataSet1.XMLData);
end;

{ 写入 XMLData 测试 }
procedure TForm1.Button2Click(Sender: TObject);
var
  str: string;
begin
  str := TFile.ReadAllText('country.xml');
  ClientDataSet1.XMLData := str;
  Memo1.Text := FormatXMLData(ClientDataSet1.XMLData);
end; 

{ 读写 Data 测试 }
procedure TForm1.Button3Click(Sender: TObject);
var
  ClientDataSet2: TClientDataSet;
begin
  ClientDataSet2 := TClientDataSet.Create(Self);
  ClientDataSet2.LoadFromFile('parts.cds');
  ClientDataSet1.Data := ClientDataSet2.Data;
  ClientDataSet2.Free;
  Memo1.Text := FormatXMLData(ClientDataSet1.XMLData);
end;

{ 合并两个数据集 }
procedure TForm1.Button4Click(Sender: TObject);
var
  cds1,cds2: TClientDataSet;
begin
  cds1 := TClientDataSet.Create(Self);
  cds2 := TClientDataSet.Create(Self);

  cds1.LoadFromFile('industry.cds');
  ShowMessage(IntToStr(cds1.RecordCount)); { 7 }

  //把 cds1 的结构与数据复制到 cds2
  cds2.Data := cds1.Data;
  ShowMessage(IntToStr(cds2.RecordCount)); { 7 }

  //把 cds1 的数据合并到 cds2
  cds2.AppendData(cds1.Data, True);
  ShowMessage(IntToStr(cds2.RecordCount)); { 14 }

  //把 cds2 的数据合并到 cds1
  cds1.AppendData(cds2.Data, True);
  ShowMessage(IntToStr(cds1.RecordCount)); { 21 }

  cds1.Free;
  cds2.Free;
end;

posted on 2010-01-20 13:24  万一  阅读(...)  评论(...编辑  收藏