Delphi - FieldByName.AsString - Good Practice

I read a blog post (at the moment I can’t find the link), where the author said that it is faster to assign a local variable than to use ADOQuery.FieldByName ('...'). asString or TSQLQuery.FieldByName ('...'). AsString when parsing an entire query with several thousand records. I do not see the difference between

var aLocalField: TField;
....
aLocalField := ADOQuery.FieldByName('...');
..
ShowMessage(aLocalField.asString)

and using directly

ShowMessage(ADOQuery.FieldByName('...').asString);

The blog post did not say anything about the type of database or version of Delphi. Is this solution related to one or another (and I'm not talking about obscure / user database systems)?

+5
source share
4 answers

There is no noticeable difference in performance between them. I expect you to find the difference between

DataSet.First;
while not DataSet.Eof do
begin
  ProcessValue(DataSet.FieldByName('Field').AsString);
  DataSet.Next;
end;

against

Field := DataSet.FieldByName('Field');
DataSet.First;
while not DataSet.Eof do
begin
  ProcessValue(Field.AsString);
  DataSet.Next;
end;

, , FieldByName () . , .

+17

, :

  • CLASS (, TForm), , , (FieldByName).

  • ( dfm), , (a) (), , , .

, , , , , "".

, :

procedure TSomething.DoSomething;
begin
    fDataset.FieldByName('X').AsString = fDataset.FieldByName('X').AsString+'Y';
end;

, , - fX:TField:

 TSomething = class(TBaseClass)
  protected
   fDataSet:TDataSet;
   fX:TField;

 end;

 fX.AsString := fX.AsString + 'Y';

, , - " " " ", " ".

+5

FieldByName uses a simple search. if you want to select more than one field. this code is better

// Same Text in StrUtils(unit)
for I:=0 to DataSet.Fields.Count-1 do 
begin
  if SameText( DataSet.Fields[I].FieldName,'Field1') then
    Field1:=DataSet.Fields[I]
  else if SameText( DataSet.Fields[I].FieldName,'Field2') then
    Field2:=DataSet.Fields[I];
end;
DataSet.First;
while not DataSet.Eof do
begin
  ProcessValue(Field.AsString);
  DataSet.Next;
end;
+1
source

All Articles