Search code examples
databaseunicodedelphi-7

Delphi7. Read BLOB field with WideString data in it


Can anybody help? I'm keeping an old Delphi7 project and have next trouble. How can I store BLOB field value if it contains Unicode string? I tried:

var
  str: WideString;
begin
  ...
  str := WideString(Fields[1].AsString); - but I get empty string
  ...

  ...
  str := VarToWideStr(Fields[1].AsVariant); - but I get "(BLOB)" result in str varible.
  ...
end;

My solution: Code usage:

...
stream := TMemoryStream.Create;
try
  Fields[1].SaveToStream(stream);
  ss := MemStreamToWStr(stream);
finally
  stream.Destroy;
end;
...

And function:

function TSnsFrame.MemStreamToWStr(Mstream: TMemoryStream): WideString;
begin
  Mstream.Seek(0, soFromBeginning);
  SetLength(Result, Mstream.size div 2);
  MStream.ReadBuffer(Result[1], Mstream.size);
end;

Solution

  • Have a look at TDataSet.CreateBlobStream(). It returns a TStream that can be used to read/write the raw data of a blob field.