内存中动态生成ClientDataSet表结构

本例演示如何在Delphi XE 中使用ClientDataSet动态创建表结构并填充数据


内存中动态生成ClientDataSet表结构_第1张图片

代码如下:

unit Form_Main;

interface

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

type
  TForm1 = class(TForm)
    DBGrid1: TDBGrid;
    DataSource1: TDataSource;
    ClientDataSet1: TClientDataSet;
    procedure FormCreate(Sender: TObject);
  private

  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
var
  i: Integer;
begin
  with ClientDataSet1 do
  begin
    FieldDefs.Add('ID', ftInteger, 0, False);
    FieldDefs.Add('Name', ftString, 10, False);
    FieldDefs.Add('Gender', ftInteger, 0, False);
    FieldDefs.Add('Age', ftInteger, 0, False);
    IndexDefs.Add('PK', 'ID', [ixPrimary, ixUnique]);
    CreateDataSet;
    DisableControls;
    for i := 1 to 1000 do
    begin
      Append;
      FieldByName('ID').AsInteger := i;
      FieldByName('Name').AsString := 'User' + IntToStr(i);
      FieldByName('Gender').AsInteger := Random(2);
      FieldByName('Age').AsInteger := Random(100);
      Post;
    end;
    EnableControls;
  end;
end;

end.


源码下载:http://www.400gb.com/file/64253035

你可能感兴趣的:(内存中动态生成ClientDataSet表结构)