Nevermore
Nevermore

Reputation: 1743

How can i add fields to TfrxDBDataset

if Length(idStrArray)>0 then
begin
with DataModule4.ADQueryTemp do
  begin
    Close;
    SQL.Clear;
    SQL.Add('SELECT id, pato, ftest, res FROM tbl ');
    SQL.Add('WHERE id IN ('+idStrArray+')');
    Open;
    (rprMasterDataFish as Tfrxmasterdata).DataSet := frxDst_Multi;
    (rprMasterDataFish as Tfrxmasterdata).DataSetName := 'Multi';

  end;
end;

Hello,

I have TfrxDBDataset component. I can add fields from table like above. But i also want to add fields and values manually at runtime.

I have text file like this :

id note

1 sample

2 sample

I want to read this text file and insert note to frxDst_Multi. Is this possible ?

I dont want to create a new column as note in tbl. Because, i have too many mysql server.

Thanks in advice,

Upvotes: 0

Views: 752

Answers (1)

MartynA
MartynA

Reputation: 30715

You can't add fields to a dataset while it is open, so you have to do it before it is opened, either in code or using the TDataSet fields editor. If you are doing it in code, you can add the field in the dataset's BeforeOpen event.

The next problem is that is you don't want to field to be bound to the table the dataset accesses, you need to add it as a calculated field and set its value in the dataset's`OnCalcFields' event - see example below.

Ideally, the added field would be a TMemoField, but unfortunately a TMemoField can't be a calculated field (FieldKind = ftMemo). So probably the best thing you can do is to make it a String field, but then you will need to give it a fixed maximum size and truncate the field's value at that size when you calculate its value.

Btw, I don't know whether your TfrxDBDataset supports the fkInternalCalc fieldkind, but if it does, then you could try adding the note field as a TMemoField instead of a TStringField one.

The one thing I haven't been able to do is to load the field's value from an external file because you haven't said in your q how to determine the name of the file which is to be read.

Obviously, the IsNull check in the frxDst_MultiCalcFields event is to avoid the overhead of reloading the file if its contents have already been read.

  const
    NoteFieldSize = 4096;

  procedure TForm1.AddNoteField;
  var
    NoteField : TField;
  begin
    if frxDst_Multi.FindField('Note') = Nil then begin
      NoteField := TStringField.Create(frxDst_Multi);
      NoteField.FieldName := 'Note';
      NoteField.Size := NoteFieldSize;
      NoteField.FieldKind := fkCalculated;
      NoteField.DataSet := frxDst_Multi;
    end;
  end;

  procedure TForm1.FormCreate(Sender: TObject);
  begin
    frxDst_Multi.Open;
  end;

  procedure TForm1.frxDst_MultiCalcFields(DataSet: TDataSet);
  var
    S : String;
  begin
    if DataSet.FieldByName('Note').IsNull then begin
      S := 'a note'; //  replace by code to set the field value
      DataSet.FieldByName('Note').AsString := Copy(S, 1, NoteFieldSize);
    end;
  end;

  procedure TForm1.frxDst_MultiBeforeOpen(DataSet: TDataSet);
  begin
    AddNoteField;
  end;

Upvotes: 2

Related Questions