delphi mysql blob,Delphi将图像另存为sql数据库中的blob

I'm trying to load a Image control from a image blob saved previously in a sql database.I have testd so many ways and i can't make it work. The image blob is saved as:

qry.SQL.Text := 'update tbl set pic = :blobVal where id = :idVal';

qry.Parameters.ParamByName('blobVal').LoadFromFile('c:\sample.jpg', ftBlob);

qry.Parameters.ParamByName('idVal').Value := 1;

any suggestion?

解决方案

There a a lot of treads here about loading images to as database, but I did not find one with update or insert parameters.

You might simply assign a graphic object to your parameter.

If you want to store different graphic types you should add a column

keeping the Information which kind of graphic should be stored (e.g. jpeg,bmp,png).

to be able to create the needed TGraphic class descendant if you want to retrieve the picture from the database.

uses jpeg, pngimage;

type

TitTYPES=(itJPG,itPNG,itBMP);

procedure TDEMO.Button1Click(Sender: TObject);

var

jp:TJpegimage;

g:TGraphic;

begin

jp:=TJpegimage.Create;

try

ads.Close;

jp.LoadFromFile('C:\Bilder1\PIC.jpg');

ads.SQL.Text := 'Insert into IMGBlob (ID,Blob,typ) Values (:ID,:BLOB,:typ)';

ads.Parameters[0].Value := 1;

ads.Parameters[1].Assign(jp);

ads.Parameters[2].Value := itJPG;

ads.ExecSQL;

ads.SQL.Text := 'Select * from IMGBlob where ID=:ID';

ads.Parameters[0].Value := 1;

ads.Open;

try

case TitTYPES(ads.FieldByName('typ').AsInteger) of

itJPG: g:=TJpegimage.Create;

itPNG: g:=TPNGImage.Create;

itBMP: g:=TBitmap.Create;

end;

g.Assign(ads.FieldByName('Blob'));

Image1.Picture.Assign(g);

finally

g.Free;

end;

finally

jp.Free;

end;

end;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值