KbmMemTable的简单应用(增删改查示例)

//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一致,以下是我简单的应用示例,实现了增删改查等基本功能:

转载于:https://www.cnblogs.com/chenmfly/p/4818321.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值