TClientDataSet的 AddIndex

unit Unit2;



interface



uses

  SysUtils, Classes, DB, DBClient;



type

  TDataModule2 = class(TDataModule)

    ClientDataSet1: TClientDataSet;

    DataSource1: TDataSource;

    procedure DataModuleCreate(Sender: TObject);

  private

    { Private declarations }

  public

    { Public declarations }

  end;



var

  DataModule2: TDataModule2;



implementation



uses

  Dialogs;



{$R *.dfm}



procedure TDataModule2.DataModuleCreate(Sender: TObject);

begin

  with DataModule2.ClientDataSet1 do

  begin

    FileName := 'test.cds';

    with TStringField.Create(DataModule2.ClientDataSet1) do

    begin

      FieldName := 'test';

      Name := 'ClientDataSet1test';

      DataSet := DataModule2.ClientDataSet1;

    end;

    CreateDataSet;

    with IndexDefs.AddIndexDef do //或者在设计时

    begin

      Name := 'Indextest';

      Fields := 'test';

      Options := [ixDescending];

    end;

//    AddIndex('IndexTest', 'test', []); 不能用这个,否则下面一句 报 

---------------------------
Project1
---------------------------
ClientDataSet1: Index 'Indextest' not found.
---------------------------
确定
---------------------------

    IndexName := 'Indextest';



    if FileExists('test.cds') then

      LoadFromFile('test.cds')

  end;

end;



end.

 

 

unit Unit1;



interface



uses

  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

  Dialogs, StdCtrls, DB, DBClient, Grids, DBGrids;



type

  TForm1 = class(TForm)

    Button1: TButton;

    DBGrid1: TDBGrid;

    procedure Button1Click(Sender: TObject);

  private

    { Private declarations }

  public

    { Public declarations }

  end;



var

  Form1: TForm1;



implementation



uses

  Unit2;



{$R *.dfm}



procedure TForm1.Button1Click(Sender: TObject);

begin

  with DataModule2.ClientDataSet1 do

  begin

    Append;

    FieldByName('test').AsString := 'test1';

    Post;

  end;

end;



end.

 

program Project1;



uses

  Forms,

  Unit1 in 'Unit1.pas' {Form1},

  Unit2 in 'Unit2.pas' {DataModule2: TDataModule};



{$R *.res}



begin

  Application.Initialize;

  Application.MainFormOnTaskbar := True;

  Application.CreateForm(TDataModule2, DataModule2);

  Application.CreateForm(TForm1, Form1);

  Application.Run;

end.

 

你可能感兴趣的:(client)