Delphi Lookup函数-根据条件查找数据

Delphi Lookup函数-根据条件查找数据

TCustomClientDataSet、TCustomADODataSet 均有

函数原型:

function Lookup(const KeyFields: string; const KeyValues: Variant;
  const ResultFields: string): Variant;
begin
  Result := Null;
  if LocateRecord(KeyFields, KeyValues, [], False) then
  begin
    SetTempState(dsCalcFields);
    try
      CalculateFields(TempBuffer);
      Result := FieldValues[ResultFields];
    finally
      RestoreState(dsBrowse);
    end;
  end;
end;

CalculateFields

procedure TDataSet.CalculateFields(Buffer: PChar);
var
  I: Integer;
begin
  FCalcBuffer := Buffer;
  if (State <> dsInternalCalc) and (IsUniDirectional = False) then
  begin
    ClearCalcFields(CalcBuffer);
    for I := 0 to FFields.Count - 1 do
      with FFields[I] do
        if FieldKind = fkLookup then CalcLookupValue;
  end;
  DoOnCalcFields;
end;

  

使用示例:

//单字段

var 
  sName:Variant;
begin
//根据ID,返回名称
  sName:=ADOQuery1.Lookup('id','22','name');
  if VarType(sName) <> varNull then
  begin
    ShowMessage(IntToStr(sName));
  end;
end; 

//多字段

var 
  sArry:Variant;
begin
//根据ID、名称,搜索返回 标题、日期
  sArry:=ADOQuery1.Lookup('id;name',VarArrayOf([22,'test']),'title;date');
  if VarType(v) <> varNull then
  begin
    ShowMessage(sArry[0]+inttostr(sArry[1]));
  end;
end;

  

 

 

创建时间:2021.01.29  更新时间:

 

上一篇:6、学生数组排序


下一篇:2、简单测试