Delphi7-TClientDataSet: Find

TClientDataSet [12]: Find

Methods:
. 1, the Locate: based on field values lookup field list and corresponding to and positioned, found Return True.
2, the Lookup:. The field value field list and the corresponding lookup returns the field value required
3, SetKey, GotoKey or SetKey , GotoNearest: lookup based on the value of the index field, the first switching state repositioning according to the conditions.
. 4, FindKey or FindNearest: lookup based on the value of the index field.

Which GotoNearest, FindNearest in the case can not be found will be positioned to approximate.

Test code:

// Preparation: Put a ClientDataSet1 and six Button on Form

{ Prepare test data } 
Procedure TForm1.FormCreate (Sender: TObject);
 the begin 
  with ClientDataSet1 do  the begin 
    FieldDefs.Add ( ' ID ' , ftInteger);
    FieldDefs.Add('Name', ftString, 6);
    FieldDefs.Add('Age', ftWord);
    CreateDataSet;
    AppendRecord ([ . 1 , ' Zhao AB ' , . 11 ]);
    AppendRecord ([ 2 , ' money AB ' , 22 is ]);
    AppendRecord ([ . 3 , ' SUN AB ' , 33 is ]);
    AppendRecord([4, '李AB', 44]);
    AppendRecord ([ . 5 , ' Zhao ab & ' , 55 ]);
    AppendRecord ([ . 6 , ' money ab & ' , 66 ]);
    AppendRecord ([ . 7 , ' SUN ab & ' , 77 ]);
    AppendRecord (, [ the 8 , ', li ab of ', , the 88- ],),;
  end The ;
end The ;

{ 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;

 

Guess you like

Origin www.cnblogs.com/jijm123/p/11269157.html