程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> TClientDataSet[12]: 查找

TClientDataSet[12]: 查找

編輯:Delphi

 方法有:

  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; 


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved