利用流来存储:
SQLDataSet1: TSQLDataSet;
该例子在DELPHI6下调试成功了.
var
Filestream:TFileStream;
StreamSize:Int64;
ArrayBuffer:Array of byte;
begin
OpenDialog1.Execute;
Filestream:=TFileStream.Create(OpenDialog1.FileName,fmOpenRead);
try
StreamSize:=Filestream.Size;
Filestream.Position:=0;
SetLength(ArrayBuffer,StreamSize);
Filestream.readbuffer(ArrayBuffer[0],StreamSize);
SQLQuery1.SQL.Clear;
SQLQuery1.SQL.Add('UPDATE es_map SET mapcon=
mapcon WHERE mapid=
mapid');
SQLQuery1.ParamByName('Pmapcon').SetBlobData(ArrayBuffer,StreamSize);
SQLQuery1.ParamByName('Pmapid').AsString:=ClientDataSet1.FieldByName('mapid').AsString;
SQLQuery1.ExecSQL;
ClientDataSet1.Refresh;
finally
Filestream.Free;
end;
读取的例子如下:
var
Stream2:TMemoryStream;
StreamSize:Int64;
ArrayBuffer:Array of byte;
BitImage:TBitmap;
JPEGImage:TJPEGImage;
begin
Stream2:=TMemoryStream.Create();
BitImage:=TBitmap.Create;
JPEGImage:=TJPEGImage.Create;
try
if ClientDataSet1.FieldByName('MAPCON').IsBlob then
begin
(ClientDataSet1.FieldByName('MAPCON') as TBlobField).SaveToStream(Stream2);
StreamSize:=Stream2.Size;
Stream2.Position:=0;
showmessage('size='+IntToStr(StreamSize));
SetLength(ArrayBuffer,StreamSize);
Stream2.readbuffer(ArrayBuffer[0],StreamSize);
Stream2.Position:=0;
if (ArrayBuffer[0]=byte('B')) and (ArrayBuffer[1]=byte('M')) then
begin
BitImage.LoadFromStream(Stream2);
DBImage1.Picture.Assign(BitImage);
end;
if (ArrayBuffer[6]=byte('J')) and (ArrayBuffer[7]=byte('F')) and (ArrayBuffer[8]=byte('I')) and (ArrayBuffer[9]=byte('F')) then
begin
JPEGImage.LoadFromStream(Stream2);
DBImage1.Picture.Assign(JPEGImage);
end;
end;
finally
BitImage.Free;
JPEGImage.Free;
Stream2.Free;
end;