简体   繁体   中英

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

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.

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;

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM