RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
TField.GetData Method

Returns the unformatted data for the field.

Pascal
function GetData(Buffer: Pointer; NativeFormat: Boolean = True): Boolean; overload;
C++
__fastcall Boolean GetData(void * Buffer, Boolean NativeFormat = True);

Call GetData to return the data in a field to a buffer. GetData is intended primarily for internal use. 

Buffer must have sufficient space allocated for the data. Use the DataSize property to determine the space required. 

Unlike the DisplayText, Text, or AsXXX properties, GetData does not translate the data into a specific type. Instead, it uses a type that reflects the underlying database information unless you set NativeFormat to false. NativeFormat indicates whether the raw database information must be translated into the underlying type associated with the specific type of database field. When NativeFormat is true, the dataset does not translate the raw database information. When NativeFormat is false, the dataset must translate the field value into the type associated with the field. 

If the data is NULL, GetData returns false and no data is transferred to Buffer. Otherwise, GetData returns true.

Note: GetData cannot be used to read data from BLOB or memo fields. To read BLOB data, use the stream returned by the dataset's CreateBlobStream method or the BLOB field's SaveToStream method.
 

C++ Examples: 

 

/*
This example requires a button, a test edit, and a populated
ClientDataSet.  Pipe the ClientDataSet through a DataSource
to a DGGrid or DBNavigator to control the current field.
Cast the data correctly according to the field type when
assigning to the test edit.
*/
void __fastcall TForm1::Button1Click(TObject *Sender)
{
  // Retrieve the "raw" data from Field1
  if (!CDS->Fields->Fields[0]->IsBlob()) // __classid(TField) GetData does not work for BLOB fields
  {
    void *MyBuffer = malloc(CDS->Fields->Fields[0]->DataSize);
    try
    {
      if (!CDS->Fields->Fields[0]->GetData(MyBuffer))
        MessageDlg(CDS->Fields->Fields[0]->DisplayName + "is blank.", mtInformation, TMsgDlgButtons() << mbOK,0);
      else
        // Do something with the data;
        Edit1->Text = reinterpret_cast<char *>(MyBuffer); // for a stringfield
    }
    __finally
    {
      free(MyBuffer);
    }
  }
}

void __fastcall TForm1::FormCreate(TObject *Sender)
{
  CDS->LoadFromFile("../CDS.XML");
}

 

Delphi Examples: 

{
This example requires a button, a test edit, and a populated
ClientDataSet.  Pipe the ClientDataSet through a DataSource
to a DGGrid or DBNavigator to control the current field.
Cast the data correctly according to the field type when
assigning to the test edit.
}

{$IFNDEF UNICODE}
uses SwSystem;
{$ENDIF}

procedure TForm1.Button1Click(Sender: TObject);
var MyBuffer: Pointer;
begin
{ Retrieve the "raw" data from Field1 }
with CDS.Fields[0] do
  begin
    if not IsBlob then { this does not work for BLOB fields }
    begin
      { Allocate space }
      MyBuffer:= GetMemory(DataSize);
      try
        if not GetData(MyBuffer) then
          MessageDlg(DisplayName + ' is NULL', mtInformation, [mbOK], 0)
        else
          { Do something with the data };
          Edit1.Text:= string(PAnsiChar(MyBuffer)); // for a stringfield
      finally
        { Free the space }
        FreeMem(MyBuffer, DataSize);
      end;
    end;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
{$IFDEF UNICODE}
  CDS.LoadFromFile(GetCurrentDir + '\CDS.XML');
{$ELSE}
  CDS.LoadFromFile(gsAppPath + 'CDS.XML');
{$ENDIF}
end;

 

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!