简体   繁体   中英

ADOQuery Get Result (Delphi)

I Tried to get result from ADOQuery in Delphi. I wrote this function for Get a Name from table according custom ID.

function GetNameByID(Id : Integer) : string;
var query : string;
  Begin
  ShowMessage(GetDBGridViewIndex().ToString);
  query := 'SELECT Name FROM Table1 WHERE ID=' + IntToStr(Id);
  With ADOQuery do
    Begin
       try
          SQL.Clear;
          SQL.Add(query);
          Open;
          First;
          Result:= // Need Get Result;
       finally
          Close;
       end;
    End;
    
    ShowMessage(result);
End;

But I don't know how can return Result from ADOQuery.

TADOQuery is a descendant of TDataset . You can iterate through the result records with the First , Next , Prior , and Last methods and find out if you've reached the end with Eof .

Within each result record you can access the fields with:

Fields[Index].AsString
Fields[Index].AsInteger
...

or

FieldByName(FieldName).AsString
FieldByName(FieldName).AsInteger
...

In this case you can access to the result using:

Result := Fields[0].AsString;
Result := FieldByName('Name').AsString;

After you Open the query, the cursor is pointing the First record. You don't need to call the First method after Open .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM