Delphi加载图像在sql数据库中另存为blob [英] Delphi load image save as blob in a sql database

查看:32
本文介绍了Delphi加载图像在sql数据库中另存为blob的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从之前保存在 sql 数据库中的图像 blob 加载图像控件.我已经测试了很多方法,但无法使其工作.图像 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;

有什么建议吗?

推荐答案

这里有很多关于将图像加载到数据库的方法,但我没有找到带有更新或插入参数的方法.

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

您可以简单地为参数分配一个图形对象.如果要存储不同的图形类型,则应添加一列保留应存储哪种图形的信息(例如 jpeg、bmp、png).如果您想从数据库中检索图片,则能够创建所需的 TGraphic 类后代.

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:Bilder1PIC.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;

这篇关于Delphi加载图像在sql数据库中另存为blob的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆