我想将一些TStringStream连接成一个Stream。
我在SQL Server中有一些blob(varbinary(max))字段,我想从所有行创建一个流,然后将其保存到文件中。
ma[0,1:,None]
由于某些原因,这不能解决问题,任何人都可以帮我提出建议吗?如何连接我的Blob字段?
编辑:
在尝试David的解决方案之后
var
// MSWord: Variant;
i: Int32;
sA: TStringStream;
sP: TStringStream;
s: TStringStream;
// fString: TStrings;
begin
sA := TStringStream.Create;
s := TStringStream.Create;
sP := TStringStream.Create;
try
with DateED do
begin
BlobField.SaveToStream(sA);
sA.SaveToStream(s);
// s.CopyFrom(sA, sA.Size);
// s.LoadFromStream(sA);
s.Position := sA.Size;
end;
with DateED.spED_TemplateParagraf do
begin
First;
while not EOF do
begin
DateED.BlobField.SaveToStream(sP);
sP.SaveToStream(s);
s.Position := sP.Size;
sP.Clear;
Next;
end;
end;
s.SaveToFile('D:\test.doc');
finally
sA.Free;
s.Free;
sP.Free;
end;
我仍然无法从字段中找到所有信息
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;
这是我打电话的方式,但在Word Dock中我只找到第一个blob字段中的文本,但实际上流大小正在增加。
答案 0 :(得分:1)
字符串流是作业的错误工具。你没有文字,你有二进制数据。您只是想连接两个二进制BLOB。用这些代码来做这件事:
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;
要保存到文件,请创建TFileStream
并将其传递给该函数。
stream := TFileStream.Create(fileName, fmCreate);
try
ConcatenateBlobFields(ds, field1, field2, stream);
finally
stream.Free;
end;
或者也许是这样:
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;