存取数据库里的图片 ( 积分: 50 )

  • 主题发起人 主题发起人 wy91267
  • 开始时间 开始时间
W

wy91267

Unregistered / Unconfirmed
GUEST, unregistred user!
我想把图片存在sqlserver2000数据库表字段里 然后能读取字段 谢谢
 
可以用ADOQUERY的Assign属性来做
比如 你的数据库里有个字段 TT为Iamge类型的
你存的时候就可以
ADOQuery.fieldbyname('tt').assign(Image.picture.Graphic)
读就相当于它的逆操作
Image.picture.Assign(adoquery.fieldbyname('tt'))
用这方法很简单的 比用什么TBlobField 或流要好点
 
应该用TBlobField
------------------------------------------------------
http://www.waibaoinfo.com 外包信息网
 
给你我的代码:
procedure TForm1.Button1Click(Sender: TObject);
var
i:Integer;
Strm:TmemoryStream;
JPG:TJPEGImage;
begin
if OpenPictureDialog1.Execute then
begin
//保存图片到数据
for i:=0 to OpenPictureDialog1.Files.Count-1do
begin
application.ProcessMessages;
try
ADOQuery1.Close;
ADOQuery1.Sql.Clear;
ADOQuery1.Sql.Add('select * from tPCStylePicData');
ADOQuery1.Open;
ADOQuery1.Append;
JPG:=TJPEGImage.Create;
JPG.CompressionQuality := 30;//压缩率
Strm:=TmemoryStream.Create ;
JPG.LoadFromFile(OpenPictureDialog1.Files.Strings);
JPG.SaveToStream(Strm);
Strm.Position:=0;
TBlobField(ADOQuery1.FieldByName('Pic')).LoadFromStream(strm);
ADOQuery1.Post;
finally
JPG.Free;
Strm.Free;
end;
end;
end ;
end;

procedure TForm1.DBGrid1CellClick(Column: TColumn);
var
Stream :TMemorystream;
JPG:TJPEGImage;
begin
Stream:= TMemorystream.Create;
try
TBlobField(ADOQuery1.FieldByName('Pic')).SaveToStream(Stream);
Stream.Position:=0;
JPG:=TJPEGImage.Create;
JPG.LoadFromStream(Stream);
Image1.Picture.Assign(JPG);
finally
JPG.Free;
Stream.Free;
end;
end;
 
我少写几个字 我问的是asp.net上的操作
 
procedure TForm1.Button1Click(Sender: TObject);
var
i:Integer;
JPG:TJPEGImage;
begin
if OpenPictureDialog1.Execute then
begin
//保存图片到数据
for i:=0 to OpenPictureDialog1.Files.Count-1do
begin
application.ProcessMessages;
try
ADOQuery1.Close;
ADOQuery1.Sql.Clear;
ADOQuery1.Sql.Add('select * from tPCStylePicData');
ADOQuery1.Open;
ADOQuery1.Append;
JPG:=TJPEGImage.Create;
JPG.CompressionQuality := 30;//压缩率
Strm:=TmemoryStream.Create ;
JPG.LoadFromFile(OpenPictureDialog1.Files.Strings);
JPG.SaveToStream(Strm);
Strm.Position:=0;
TBlobField(ADOQuery1.FieldByName('Pic')).LoadFromStream(strm);
ADOQuery1.Post;
finally
JPG.Free;
Strm.Free;
end;
end;
end ;
end;

<script type=&quot;text/javascript&quot;><!--
google_ad_client = &quot;pub-5105004733467969&quot;;
google_ad_slot = &quot;5709780502&quot;;
google_ad_width = 728;
google_ad_height = 90;
<script type=&quot;text/javascript&quot;
src=&quot;http://pagead2.googlesyndication.com/pagead/show_ads.js&quot;>
</script>
 
www.photo88.cn
 
我用vs2005.net开发 数据库为sqlserver2000 做个网页能上传图片并保存到数据库里 并能下载
 
我也不會。學習一下。謝謝、。
 
var
aMStream: TMemoryStream;
aBStream: TBlobStream;
str :String;
begin
str:='c:/hello.jpg';
aMStream := TMemoryStream.Create();
try
aMStream .LoadFromFile(str);
ADOTable1.Append;
aBStream := TBlobStream.Create(TBlobField(ADOTable1.FieldByName('jpg')), bmWrite);
aMStream.SaveToStream(aBStream );
ADOTable1.Post;
finally
aMStream .Free;
end;
end;



** 读
var
aJPG: TJpegImage;
aBStream: TBlobStream;
str :String;
begin
str:='c:/hello.jpg';
if ADOTable1.FieldByName('jpg').IsNull then
begin
Application.MessageBox('无图像!','提示',MB_IconInformation+MB_OK);
Exit;
end;
aJPG := TJPEGImage.Create();
with aJPGdo
try
aBStream := TBlobStream.Create(TBlobField(ADOTable1.FieldByName('Bmp')), bmRead);
try
aJPG.LoadFromStream(aBStream);
aJPG.SaveToFile(str);
finally
aBStream.Free;
end;
finally
aJPG.Free;
end;
end;
 
后退
顶部