Вопрос

The scenario is I have an IbQuery that is dynamically created with many possible variations of fields. After its execution I want to test to see if one specific field was included and has data. Something similar to this:

ibqry.fetchall();

while not ibqry.eof do
begin
if (ibqry.FieldByName(‘Lastname’) <> null)
    lname := ibqry. FieldByName(‘Lastname’).tostring()
else
    lname := ‘’

ect....
end

How can I do this. Thanks.

Это было полезно?

Решение

TDataSet.FieldByName raises an exception if you try to access a field that isn't in the dataset.

You can use TDataSet.FindField instead. It returns a reference to the field if it is present, or nil if it isn't. Something like this should work for you:

var
  LastNameField: TField;
begin
  LastNameField := ibqry.FindField('LastName');

  ibqry.fetchall();

  while not ibqry.Eof do
  begin
    lName := '';
    if Assigned(LastNameField) then
      if not LastNameField.IsNull then
        lName := LastNameField.AsString;

    // Do whatever with query content

    ibqry.Next;
  end;
end;
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top