在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: string; const 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;
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: string; const 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]);