Delphi Examples: DbiGetField

<< Click to Display Table of Contents >>

Navigation:  »No topics above this level«

Delphi Examples: DbiGetField

Return to chapter overview

Retrieve the data contents of the requested field from the record buffer:

Delphi users should not need to directly call dbiGetField because Delphi provides a variety of ways to retrieve the value of a particular field. Use the Delphi online help to browse the Value and As... properties of TField. Also see the FieldValues[] array property of TTable.

Get a field in a table and return it in a Variant type variable.

Some field types are not supported and will cause an exception. Most Delphi users should use TField objects to retrieve table information. This example uses the following input:

 MStr := fDbiGetField(Table1.Handle, Table1.Fields[0].Index + 1);

 

The function is:

function fDbiGetField(hTmpCur: hDBICur; FieldNo: Word): Variant;

var

 Props: CURProps;

 pFlds, pOldFlds: pFLDDesc;

 pRecBuf: pBYTE;

 FieldString: String;

 FieldINT16: Smallint;

 FieldINT32: Longint;

 FieldUINT16: Word;

 FieldFLOAT: Double;

 Blank: Boolean;

begin

if (FieldNo < 1) then

  raise EDatabaseError.Create('Field number index is 1 based');

 Check(DbiGetCursorProps(hTmpCur, Props));

 pFlds := AllocMem(Props.iFields * sizeof(FLDDesc));

 pOldFlds := pFlds;

 pRecBuf := AllocMem(Props.iRecBufSize * sizeof(BYTE));

try

   Check(DbiGetFieldDescs(hTmpCur, pFlds));

   Inc(pFlds, FieldNo - 1);

   Check(DbiGetRecord(hTmpCur, dbiNOLOCK, pRecBuf, nil));

  case pFlds.iFldType of

     fldDATE, fldTIME, fldTIMESTAMP, fldUNKNOWN, fldBLOB, fldBOOL, fldBCD:

      raise EDBEngineError.Create(DBIERR_NOTSUPPORTED);

     fldZSTRING:

    begin

       SetLength(FieldString, pFlds.iUnits1 + 1);

       Check(DbiGetField(hTmpCur, FieldNo, pRecBuf,

         pBYTE(PChar(FieldString)), Blank));

       SetLength(FieldString, StrLen(PChar(FieldString)));

       Result := FieldString;

    end;

     fldINT16:

    begin

       Check(DbiGetField(hTmpCur, FieldNo, pRecBuf, pBYTE(@FieldINT16),

         Blank));

       Result := FieldINT16;

    end;

     fldUINT16:

    begin

       Check(DbiGetField(hTmpCur, FieldNo, pRecBuf, pBYTE(@FieldUINT16),

         Blank));

       Result := FieldUINT16;

    end;

     fldFLOAT:

    begin

       Check(DbiGetField(hTmpCur, FieldNo, pRecBuf, pBYTE(@FieldFLOAT),

         Blank));

       Result := FieldFLOAT;

    end;

     fldINT32, fldUINT32:

    begin

       Check(DbiGetField(hTmpCur, FieldNo, pRecBuf, pBYTE(@FieldINT32),

         Blank));

       Result := FieldINT32;

    end;

  end;

finally

   FreeMem(pOldFlds, Props.iFields * sizeof(FLDDesc));

   FreeMem(pRecBuf, Props.iRecBufSize * sizeof(BYTE));

end;

end;