码迷,mamicode.com
首页 > Windows程序 > 详细

Delphi7-TClientDataSet: 查找

时间:2019-07-30 12:56:23      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:proc   窗体   code   条件   goto   for   dataset   delphi7   代码   

TClientDataSet[12]: 查找

方法有:
1、Locate: 根据字段列表和对应的字段值查找并定位, 找到返回 True.
2、Lookup: 根据字段列表和对应的字段值查找, 返回需要的字段值.
3、SetKey、GotoKey 或 SetKey、GotoNearest: 根据索引字段的值查找, 先切换状态再根据条件定位.
4、FindKey 或 FindNearest: 根据索引字段的值查找.

其中的 GotoNearest、FindNearest 在找不到的情况下会定位到近似值.

测试代码:

//准备: 窗体上放一个 ClientDataSet1 和六个 Button

{ 准备测试数据 }
procedure TForm1.FormCreate(Sender: TObject);
begin
  with ClientDataSet1 do begin
    FieldDefs.Add(ID, ftInteger);
    FieldDefs.Add(Name, ftString, 6);
    FieldDefs.Add(Age, ftWord);
    CreateDataSet;
    AppendRecord([1, 赵AB, 11]);
    AppendRecord([2, 钱AB, 22]);
    AppendRecord([3, 孙AB, 33]);
    AppendRecord([4, 李AB, 44]);
    AppendRecord([5, 赵ab, 55]);
    AppendRecord([6, 钱ab, 66]);
    AppendRecord([7, 孙ab, 77]);
    AppendRecord([8, 李ab, 88]);
  end;
end;

{ Locate 测试 }
procedure TForm1.Button1Click(Sender: TObject);
begin
  if ClientDataSet1.Locate(Name, 赵ab, []) then
    ShowMessage(ClientDataSet1.FieldValues[Age]); { 55 }

  if ClientDataSet1.Locate(Name, 赵ab, [loCaseInsensitive]) then
    ShowMessage(ClientDataSet1.FieldValues[Age]); { 11 }

  if ClientDataSet1.Locate(Name, 钱a, [loPartialKey]) then
    ShowMessage(ClientDataSet1.FieldValues[Age]); { 66 }

  if ClientDataSet1.Locate(Name, 钱a, [loCaseInsensitive,loPartialKey]) then
    ShowMessage(ClientDataSet1.FieldValues[Age]); { 22 }

  if ClientDataSet1.Locate(Name;Age, VarArrayOf([钱ab,66]), []) then
    ShowMessage(ClientDataSet1.FieldValues[Age]); { 66 }
end;

{ Lookup 测试 }
procedure TForm1.Button2Click(Sender: TObject);
var
  R: Variant;
  i: Integer;
begin
  R := ClientDataSet1.Lookup(Name, 钱AB, Age);
  if not VarIsNull(R) then ShowMessage(R); { 22 }

  R := ClientDataSet1.Lookup(Name;Age, VarArrayOf([钱ab,66]), Age);
  if not VarIsNull(R) then ShowMessage(R); { 66 }

  R := ClientDataSet1.Lookup(ID, 6, Name;Age);
  if VarIsArray(R) then
    for i := VarArrayLowBound(R, 1) to VarArrayHighBound(R, 1) do
      ShowMessage(R[i]); { 钱ab / 66}
end;

{ SetKey、GotoKey 测试 }
procedure TForm1.Button3Click(Sender: TObject);
begin
  ClientDataSet1.IndexFieldNames := Name;
  ClientDataSet1.SetKey;
  ClientDataSet1.FieldValues[Name] := 钱ab;
  if ClientDataSet1.GotoKey then
    ShowMessage(ClientDataSet1.FieldValues[Age]); { 66 }
end;

{ SetKey、GotoNearest 测试 }
procedure TForm1.Button4Click(Sender: TObject);
begin
  ClientDataSet1.IndexFieldNames := Name;
  ClientDataSet1.SetKey;
  ClientDataSet1.FieldValues[Name] := ;
  ClientDataSet1.GotoNearest;
  ShowMessage(ClientDataSet1.FieldValues[Age]); { 77 }
end;

{ FindKey 测试 }
procedure TForm1.Button5Click(Sender: TObject);
begin
  ClientDataSet1.IndexFieldNames := Name; Age;

  if ClientDataSet1.FindKey([赵ab]) then
    ShowMessage(ClientDataSet1.FieldValues[Age]); { 55 }

  if ClientDataSet1.FindKey([赵AB, 11]) then
    ShowMessage(ClientDataSet1.FieldValues[Age]); { 11 }
end;

{ FindNearest 测试 }
procedure TForm1.Button6Click(Sender: TObject);
begin
  ClientDataSet1.IndexFieldNames := Name;
  ClientDataSet1.FindNearest([]);
  ShowMessage(ClientDataSet1.FieldValues[Age]); { 55 }
end;

 

Delphi7-TClientDataSet: 查找

标签:proc   窗体   code   条件   goto   for   dataset   delphi7   代码   

原文地址:https://www.cnblogs.com/jijm123/p/11269157.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!