Reading DataSet structure without reading it's data

ado, calculated-field, dataset, delphi, field

Solution

You can use DataSet.FieldDefs.Update method. This will still involve some data transfer but no rows will be fetched. You can call this method in the BeforeOpen event of the TDataSet and also add the calculated fields there.

Here's a short example that works for me:

procedure TDataModule.cdsExampleBeforeOpen(DataSet: TDataSet);
var I: Integer;
    TmpField: TDateTimeField;
begin
  // Get field definitions from the server
  DataSet.FieldDefs.Update;

  // Add calculated field
  TmpField := TDateTimeField.Create(DataSet);
  with TmpField do
  begin
    Name := 'Date';
    FieldName := 'Date';
    DisplayLabel := 'Date';
    DisplayFormat := 'ddd ddddd';
    Calculated := True;
  end;
  TmpField.DataSet := DataSet;

  // Create fields from field definitions
  for I := 0 to DataSet.FieldDefs.Count - 1 do
    DataSet.FieldDefs[I].CreateField(DataSet);
end;

Problem

Scenerio: I want to add a calculated field to given (any) dataset during runtime. I don't know any other way to obtain a dataset structure other than performing `DataSet.Open` method. But the `Open` method causes that atleast one row of a data needs to be transfered from server to client. Then I need to close the DataSet, add field and reopen it. This is an unnecessery overhead in my opinion. Is there a better way of doing this? Please not that I want to be able adding a calcuated field to any dataset and I don't know its structure prior to opening. In pseudocode it looks like this: ``` DataSet.Open; DataSet.Close; RecreateFieldsStructure; AddCalculatedField; DataSet.Open; ``` Thanks for your time.

Original source