用TClientDataset做内存数据集
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, DB, DBClient, Grids, DBGrids;
type
TForm1 = class(TForm)
ButtonLoad: TButton;
ButtonSave: TButton;
DataSource1: TDataSource;
DBGrid1: TDBGrid;
ButtonAddData: TButton;
procedure FormCreate(Sender: TObject);
procedure ButtonLoadClick(Sender: TObject);
procedure ButtonSaveClick(Sender: TObject);
procedure ButtonAddDataClick(Sender: TObject);
private
{ Private declarations }
Table: TClientDataSet;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.ButtonLoadClick(Sender: TObject);
begin
table.LoadFromFile('c:/mem.cds');
end;
procedure TForm1.ButtonSaveClick(Sender: TObject);
begin
table.SaveToFile('c:/mem.cds');
end;
procedure TForm1.ButtonAddDataClick(Sender: TObject);
var
i: integer;
begin
for i := 1 to 100do
begin
table.Append;
table.FieldByName('ID').AsInteger := i;
table.FieldByName('Status').AsString := 'Code'+IntToStr(i);
table.FieldByName('Created').AsDateTime := Date();
table.FieldByName('Volume').AsFloat := Random(10000);
table.Post;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
table := TClientDataset.Create(Application);
table.FieldDefs.Add('ID', ftInteger, 0, False);
table.FieldDefs.Add('Status', ftString, 10, False);
table.FieldDefs.Add('Created', ftDate, 0, False);
table.FieldDefs.Add('Volume', ftFloat, 0, False);
table.CreateDataset;
table.Open;
table.IndexFieldNames := 'ID';//动态创建索引
DataSource1.DataSet:=table;
end;
end.