Skip to content Skip to sidebar Skip to footer

Concatenate Tstringstream

I would like to concatenate some TStringStreams into one Stream. I have some blob (varbinary(max)) fields in SQL Server and and I want to create a stream from all the rows then sav

Solution 1:

A string stream is the wrong tool for the job. You don't have text, you have binary data. You are simply looking to concatenate two binary BLOBs. Do that with code something along these lines:

procedure ConcatenateBlobField(ds: TDataSet; field1, field2: TBlobField; outputStream: TStream);
var
  inputStream: TStream;
begin
  inputStream := ds.CreateBlobStream(field1, bmRead);
  try
    outputStream.CopyFrom(inputStream, inputStream.Size);
  finally
    inputStream.Free;
  end;

  inputStream := ds.CreateBlobStream(field2, bmRead);
  try
    outputStream.CopyFrom(inputStream, inputStream.Size);
  finally
    inputStream.Free;
  end;
end;

In order to save to a file, create a TFileStream and pass it to the function.

stream := TFileStream.Create(fileName, fmCreate);
tryConcatenateBlobFields(ds, field1, field2, stream);
finally
  stream.Free;
end;

Or perhaps like this:

procedure CopyBlobFieldToStream(ds: TDataSet; field: TBlobField; outputStream: TStream);
var
  inputStream: TStream;
begin
  inputStream := ds.CreateBlobStream(field, bmRead);
  try
    outputStream.CopyFrom(inputStream, inputStream.Size);
  finally
    inputStream.Free;
  end;
end;

....

stream := TFileStream.Create(fileName, fmCreate);
try
  CopyBlobFieldToStream(ds, field1, stream);
  CopyBlobFieldToStream(ds, field2, stream);
finally
  stream.Free;
end;

Post a Comment for "Concatenate Tstringstream"