//kbmMemTable unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, Grids, DBGrids, DB, StdCtrls, kbmMemTable; type TForm1 = class(TForm) Button1: TButton; DataSource1: TDataSource; DBGrid1: TDBGrid; Button2: TButton; Button3: TButton; Button4: TButton; Button5: TButton; Button6: TButton; Button7: TButton; procedure Button1Click(Sender: TObject); procedure FormCreate(Sender: TObject); procedure Button2Click(Sender: TObject); procedure Button4Click(Sender: TObject); procedure Button3Click(Sender: TObject); procedure Button5Click(Sender: TObject); procedure Button6Click(Sender: TObject); procedure Button7Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; kbmMemTable1 : TkbmMemTable; Id:Integer; implementation {$R *.dfm} procedure TForm1.Button1Click(Sender: TObject); begin //建表 kbmMemTable1.FieldDefs.Clear; kbmMemTable1.FieldDefs.Add('Id', ftInteger, 0, False); kbmMemTable1.FieldDefs.Add('Value', FtString, 20, False); kbmMemTable1.FieldDefs.Add('Time', ftDateTime, 0, False); kbmMemTable1.IndexDefs.Add('Index1','Id',[]);//定义索引 kbmMemTable1.CreateTable; kbmMemTable1.Active := True; end; procedure TForm1.Button2Click(Sender: TObject); var fld_Id:TIntegerField; fld_Value:TStringField; fld_Time:TDateTimeField; i:Integer; begin //插入 with kbmMemTable1 do begin DisableControls; //切断数据感知控件 try Open; //打开 //定义Field fld_Id:=TIntegerField(FieldByName('Id')); fld_Value:=TStringField(FieldByName('Value')); fld_Time:=TDateTimeField(FieldByName('Time')); for i := 0 to 9 do begin Inc(Id); Append; //附加数据 //赋值 fld_Id.AsInteger := Id; fld_Value.AsString := 'Hello PFeng!'+inttostr(Id); fld_Time.AsDateTime := Now; Post; //确定 end; UpdateIndexes;//更新索引 finally EnableControls; //连接数据感知控件 end; end; end; procedure TForm1.Button3Click(Sender: TObject); var fld_Id:TIntegerField; fld_Value:TStringField; fld_Time:TDateTimeField; begin //更新 with kbmMemTable1 do begin Open; fld_Id:=TIntegerField(FieldByName('Id')); fld_Value:=TStringField(FieldByName('Value')); fld_Time:=TDateTimeField(FieldByName('Time')); if Locate('Id',5,[]) then begin Edit; fld_Value.AsString := '内容被修改'; fld_Time.AsDateTime := Now; Post; end; end; end; procedure TForm1.Button4Click(Sender: TObject); var fld_Id:TIntegerField; fld_Value:TStringField; fld_Time:TDateTimeField; begin //查询 with kbmMemTable1 do begin Open; fld_Id:=TIntegerField(FieldByName('Id')); fld_Value:=TStringField(FieldByName('Value')); fld_Time:=TDateTimeField(FieldByName('Time')); if Locate('Id',5,[]) then ShowMessage(fld_Value.AsString); //还可以用FindKey实现,结合索引速度更快 // kbmMemTable1.IndexFieldNames:='Id'; // if kbmMemtable1.FindKey([5]) then ... end; end; procedure TForm1.Button5Click(Sender: TObject); var fld_Id:TIntegerField; fld_Value:TStringField; fld_Time:TDateTimeField; begin //删除 with kbmMemTable1 do begin Open; fld_Id:=TIntegerField(FieldByName('Id')); fld_Value:=TStringField(FieldByName('Value')); fld_Time:=TDateTimeField(FieldByName('Time')); if Locate('Id',5,[]) then begin Delete; UpdateIndexes; end; end; end; procedure TForm1.Button6Click(Sender: TObject); var fld_Id:TIntegerField; fld_Value:TStringField; fld_Time:TDateTimeField; id:Integer; begin //循环比较最小值 with kbmMemTable1 do begin Open; fld_Id:=TIntegerField(FieldByName('Id')); fld_Value:=TStringField(FieldByName('Value')); fld_Time:=TDateTimeField(FieldByName('Time')); DisableControls; try First; id := fld_Id.AsInteger; while not Eof do begin if id > fld_Id.AsInteger then id := fld_Id.AsInteger; Next; end; Locate('Id',id,[]); finally EnableControls; end; end; end; procedure TForm1.Button7Click(Sender: TObject); begin //清空 kbmMemTable1.EmptyTable; end; procedure TForm1.FormCreate(Sender: TObject); begin kbmMemTable1 := TkbmMemTable.Create(Self); DataSource1.DataSet := kbmMemTable1; end; end.
KbmMemTable是一款高效且强大的内存表控件,内存表顾名思义其最大的优势就是速度,KbmMemTable不仅完美地实现了高效的特征,同时,最新版本还支持索引、SQL语句等高级用法。其用法超简单,基本用法和ClientDataSet一致,以下是我简单的应用示例,实现了增删改查等基本功能: