怎样把图片保存到数据库中啊?(附带SQL2000问题) (88分)

  • 主题发起人 主题发起人 幸福鸟
  • 开始时间 开始时间

幸福鸟

Unregistered / Unconfirmed
GUEST, unregistred user!
你们是怎样把图片保存到数据库中的啊,用ADO解答一下吧??
 
if OpenDialog1.Execute then
begin
DM1.ADOQuery1.Append;
DM1.ADOQuery1.FieldByName('PHOTO') as TBlobField).LoadFromFile(OpenDialog1.FileName);
DM1.ADOQuery1.Post;
end;
 
procedure TFm_Person.N8Click(Sender: TObject);
var
ADOCommand:TADOCommand;
TmpStr:String;
begin
if OpenPictureDialog1.Execute then
begin
Form1.ADOConn.BeginTrans;
try
ADOCommand:=TADOCommand.Create(self);
ADOCommand.Connection:=Form1.ADOConn;
TmpStr:='Delete From PersonData_Image Where Person_id='+Lb_id.Caption+#13;
TmpStr:=TmpStr+'Insert Into PersonData_Image (Person_id,照片) Values (:Person_id,:照片)';
ADOCommand.CommandText:=TmpStr;
ADOCommand.Parameters.ParamByName('Person_id').Value:=Lb_id.Caption;
ADOCommand.Parameters.ParamByName('照片').LoadFromFile(OpenPictureDialog1.FileName,ftBlob);//(ADOBS,ftBlob);
ADOCommand.Execute;
ADOCommand.Free;
Form1.ADOConn.CommitTrans;
Image1.Picture.LoadFromFile(OpenPictureDialog1.FileName);
except
on E:Exception do
begin
Form1.ADOConn.RollbackTrans;
ADOCommand.Free;
Application.MessageBox(PChar('操作遇到错误被终止,系统返回的错误信息如下:'+#13+#13+E.Message) ,'操作失败',Mb_OK+Mb_IconInformation);
end;
end;
end;
end;
 
procedure TForm16.Button1Click(Sender: TObject);
var Graphic1:TGraphic;
begin
Graphic1:=TBitmap.Create;//因为TGraphic是TBitmap, TIcon等类的抽象类,所以Graphic1不能用TGraphic来创建

if form16.OpenPictureDialog1.FileName<>'' then
Graphic1.LoadFromFile(Form16.OpenPictureDialog1.FileName);
with Form16.ADOTable1 do
begin
open;
Insert;
FieldByName('vc_ReadCode').AsString:=dbCode.Text;
FieldByName('vc_ReadName').AsString:=dbName.Text;
TBlobField(FieldByName('img_image')).Assign(Graphic1);

ADOTable1.Post;
end; //end with


if form16.OpenPictureDialog1.FileName<>'' then
begin
imgPhoto.Picture:=nil;
Graphic1.Free;
end;
form16.OpenPictureDialog1.FileName:='';
ADOTable1.Close;

end;
 
loadfromfile/loadfromstream应该不难的
 
多人接受答案了。
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
后退
顶部