Delphi - How to get OldValue and NewValue dataset for blob field as streams

In Delphi XE, I use OldValue and NewValue from TpFIBDataSet (but it can be applied to any TDataSet descendant) to check if different fields have changed. My question is, how can I get these 2 values ​​for blob fields like TMemoryStreams? I did some research but found nothing.

+4
source share
2 answers

I use the procedure to determine if a field has changed, so I can restrict the fields that I send to the database to only those that have changed. Today I added the code for this routine to handle BLOB-fields, as they can not be returned .AsVariant, where the back OldValueand NewValue. I have not tested this with all my use cases, but so far it seems pretty solid.

function FieldChanged(DataSet: TDataSet; FieldName: string): Boolean;
var
  fld: TField;
begin
  fld := DataSet.FieldByName(FieldName);

  if fld.IsBlob then
    Exit((fld as TBlobField).Modified);

  if (fld.OldValue = Null) and (fld.NewValue = Unassigned) then // This happens when a NULL field does not change
    Exit(False)
  else
    Exit(fld.OldValue <> fld.NewValue);
end;
+2
source

I do like this:

var
  stream: TBytesStream;
begin
  if not DataSet.FieldByName('blobfield').IsNull then
  begin
    stream := TBytesStream.Create(DataSet.FieldByName('blobfield').AsBytes);
    // do something with the stream
    FreeAndNil(stream);
  end;
end;

I prefer to use TBytesStreamit because it does the same things that I usually use TMemoryStreamfor, and if you look at its constructor, it does not redistribute the memory and copy binary data to it, as TMemoryStreamit does.

TMemoryStream, TBytesStream TMemoryStream TBytesStream.SaveToStream().

0

All Articles