RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
TField.DataSize Property

Indicates the amount of memory needed to store a field component's value.

Pascal
property DataSize: Integer;
C++
__property int DataSize;

Check DataSize to determine the number of bytes required to store a field component's value. Use DataSize to determine the required size of a buffer for working with the field's value in native format. For example, use DataSize to determine the buffer size needed by the GetData and SetData methods. 

The value of DataSize for TField is 0. Most descendants of TField override this property to specify the size required by the specific field type they represent. BLOB fields, however, represent data of indeterminate size. They do not override the inherited method, so the value of DataSize for BLOB fields is 0. A value of zero should not be interpreted as meaning the field value requires no memory. A value of zero indicates that the memory required for the field is indeterminate.  

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!