TClientDataSet的操作

记性不好了,唉,这里写个被忘
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Grids, DBGrids, DB, DBClient, StdCtrls;

type
  TForm1 
= class(TForm)
    Button1: TButton;
    Button2: TButton;
    ClientDataSet1: TClientDataSet;
    DataSource1: TDataSource;
    DBGrid1: TDBGrid;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  
private
    
{ Private declarations }
  
public
    
{ Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button2Click(Sender: TObject);
begin
  ClientDataSet1.LoadFromFile(
'E:\TEMP.XML');
end;


procedure TForm1.Button1Click(Sender: TObject);
var  tempClientDataSet : TClientDataSet;
begin
tempClientDataSet :
= TClientDataSet.Create(self);
try    with tempClientDataSet do
  begin
        with FieldDefs.AddFieldDef 
do
        begin
               DataType :
= ftInteger;
               Name     :
= 'Field1';
        end;
        with FieldDefs.AddFieldDef 
do
        begin
               DataType :
= ftdatetime;
               Name     :
= 'Field2';
        end;
        createDataSet;
        appendRecord([
3,now]);
        appendRecord([
1,now]);
        appendRecord([
2,now]);
        tempClientDataSet.AddIndex(
'T1''Field1', []);
        tempClientDataSet.IndexName :
= 'T1';
        first;
        
while not EOF do
          begin
              showmessage(intToStr(FieldByName(
'Field1').AsInteger)+formatdatetime('yyyymmdd HH:NN:DD',FieldByName('Field2').AsDateTime ));
              next;
          end;
        end;
        tempClientDataSet.SaveToFile(
'E:\TEMP.XML');
  
finally
  tempClientDataSet.Free;
end;
end;



end.
原文地址:https://www.cnblogs.com/enli/p/642359.html