delphi image图片如何插入到数据库

delphi image图片如何插入到数据库,第1张

以下代码是复制来的,做的是update的 *** 作,insert也是大同小异。

最重要的是使用Blob字段用来存储你的照片

procedure TfrmStudentInfoManage.Label10Click(Sender: TObject)

var

Ms:TmemoryStream

jpg:Tjpegimage

begin

if OpenPictureDialog1.Execute then

begin

Image1.Picture.LoadFromFile(OpenPictureDialog1.FileName)

ms:=TmemoryStream.Create

Jpg:=TJpegImage.Create

Jpg.Assign(Image1.Picture.Graphic)

Jpg.SaveToStream(Ms)

Ms.Position :=0

adoquery1.Close

adoquery1.SQL.Clear

ADOQuery1.SQL.Add('update StudentInfoData set 照片=:照片 where 学号=:学号')//保存图片到数据库

adoquery1.Parameters.ParamByName('学号').Value:=DBEdit1.Text

adoQuery1.Parameters.ParamByName('照片').LoadFromStream(ms,ftBlob)

adoquery1.ExecSQL

Ms.Free

jpg.free

end

else

begin

Application.MessageBox('保存出错或文件名无效!','出错',64)

exit

end

end

var Stream:TMemoryStream

begin

try

Stream := TMemoryStream.Create// 创建内存流Image1.Picture.Graphic.SaveToStream(Stream) // 将图片保存到内存流中

adoquery1.Close adoquery1.SQL.Clear adoQuery1.SQL.Add('Insert into test3 values (:id,:photo)') ADOQuery1.Parameters.ParamByName('id').Value := editId.Text adoQuery1.Parameters.ParamByName('photo').LoadFromStream(Stream,ftBlob)// 读取保存的内存图adoquery1.ExecSQL

finally

Stream.Free// 释放内存流

end

end


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/sjk/6703142.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-03-27
下一篇2023-03-27

发表评论

登录后才能评论

评论列表(0条)

    保存