Delphi7-TClientDataSet: 查找

Posted jijm123

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Delphi7-TClientDataSet: 查找相关的知识,希望对你有一定的参考价值。

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: 查找的主要内容,如果未能解决你的问题,请参考以下文章

jsp为啥要用增删改查

java 程序 增删改查

怎么查两个地方的距离?

如何进行论文查重

查手机归属地怎么查

查端口占用,查进程号,杀进程