三层中怎么向SQLSERVER写入图片和取出图片?急(300分)

  • 主题发起人 主题发起人 zzutlink
  • 开始时间 开始时间
这边回答的都是两层的方法,三层根本不是这么回事,兄弟可能你只能试试用方法了。传byte型值,我没试过
 
值得关注!
 
通过image控件和dbimage 控件的互相转换就可以!
 
试试我的吧:(在win98+delphi6下通过)
var
PictureStream:TADOBlobStream;
jpg:TJPEGImage;
begin
PictureStream:=TADOBlobStream.Create(ADOTable1.FieldByName('照片') as TBlobField,BMRead);
jpg:=TJPEGImage.Create;
jpg.LoadFromStream(PictureStream);
Image1.Picture.BitMap.Assign(jpg);
PictureStream.Free;
jpg.Free;
end;
以上是取出图片的代码,写入时只要作相应改变即可。
 
to tintin2010:
clientdataset支持流读写的,三层一样,自己试试。
用远程调用当然可以,不过要用一对转换函数来进行OLEVARIANT类型转换,比起前者麻烦的多。
 
好像还没有一个真正解决问题得可行得答案!
 
用OLEVariant传递
 
还是不行!唉.................
 
利用流来存储:
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=:pmapcon WHERE mapid=:Pmapid');
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;
 
几年前写的一段,请参考
{read}
procedure Tfmfrmdtl.pm_changeClick(Sender: TObject);
var picname,picext:string;
p:pchar;
bs:tadoblobstream;
len:integer;
fs:tfilestream;
begin
opdpic.FileName:='';
p:=nil;
if opdpic.Execute then
begin
if fileexists(opdpic.FileName) then
begin
picname:=opdpic.FileName;
if getalterpic(opdpic.filename)=mrok then
begin
try
with dmmisnet.aqpicbymnodo
begin
if state=dsinactive then
exit;
edit;
picext:=extractfileext(picname);
fieldbyname('filename').value:=fieldbyname('memberno').value+picext;
bs:=tadoblobstream.Create(tblobfield(fieldbyname('picture')),bmwrite);
fs:=tfilestream.Create(picname,fmopenread);
with fsdo
try
len:=size;
p:=stralloc(len);
read(p^,len);
bs.Write(p^,len);
finally
strdispose(p);
fs.Free;
bs.Free;
end;
post;
end;
except
application.ShowException(exception(exceptobject));
exit;
end;
imgpic.Picture.LoadFromFile(picname);
end;
end;
end;
end;

{write}
procedure Tfmfrmdtl.pm_saveClick(Sender: TObject);
var picname:string;
p:pchar;
bs:tadoblobstream;
len:integer;
fs:tfilestream;
begin
sdpic.FileName:='';
fs:=nil;
if sdpic.Execute then
begin
picname:=sdpic.FileName;
with dmmisnet.aqpicbymnodo
begin
if state=dsinactive then
active:=true;
bs:=tadoblobstream.Create(tblobfield(fieldbyname('picture')),bmread);
try
len:=bs.Size;
p:=AllocMem(len);
try
bs.Read(p^,len);
fs:=tfilestream.Create(picname,fmcreate);
fs.Write(p^,len);
finally
FreeMem(p,len);
fs.Free;
end;
finally
bs.Free;
end;
end;
end;
end;
 

Similar threads

D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
后退
顶部