博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

ADO_Locate(数据查找与定位)

Posted on 2012-02-02 12:13  豪杰的爸爸  阅读(1814)  评论(0编辑  收藏  举报

在Delphi中,Locate和Lookup封装了原生ADO的Find查找方法。提供以下函数形式使用

 

 1 function TCustomADODataSet.Locate(const KeyFields: string;
 2   const KeyValues: Variant; Options: TLocateOptions): Boolean;
 3 begin
 4   DoBeforeScroll;
 5   Result := LocateRecord(KeyFields, KeyValues, Options, True);
 6   if Result then
 7   begin
 8     Resync([rmExact, rmCenter]);
 9     DoAfterScroll;
10   end;
11 end;
12 
13 function TCustomADODataSet.Lookup(const KeyFields: stringconst KeyValues: Variant;
14   const ResultFields: string): Variant;
15 begin
16   Result := Null;
17   if LocateRecord(KeyFields, KeyValues, [], False) then
18   begin
19     SetTempState(dsCalcFields);
20     try
21       CalculateFields(TempBuffer);
22       Result := FieldValues[ResultFields];
23     finally
24       RestoreState(dsBrowse);
25     end;
26   end;
27 end;

使用时 loCaseInsensitive代表区分大小写,loPartialKe代表部分匹配。

当多个字段时用“;”隔开字段名,字段值是变体数组形式,可以用VarArrayof()生成。

例如:ds1.Locate('name', 'k1727', [loCaseInsensitive, loPartialKe]);