Question

On Delphi XE I'm using OldValue and NewValue from TpFIBDataSet (but it can be applied to any TDataSet descendant) in order to verify if different fields have changed. My question is how can I retrieve these 2 values for blob fields as TMemoryStreams? I've made some research, but I found nothing.

Was it helpful?

Solution

I use a routine to detect if a field changed so I can limit the fields I send to the database to only the ones that changed. I added code to that routine today to handle BLOB fields, since they cannot be returned .AsVariant, which is how OldValue and NewValue are returned. I have not tested this with all of 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;

OTHER TIPS

I do it this way:

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 using TBytesStream because it accomplishes the same things I typically use a TMemoryStream for, and if you look at its constructor, it doesn't reallocate memory and copy the binary data to it like TMemoryStream does.

If you really need to use TMemoryStream, you could easily save the TBytesStream to the TMemoryStream using the TBytesStream.SaveToStream() method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top