delphi刚入门,请教各位大侠一个问题,如何用tquery将图片存入Access数据库

解决方案 »

  1.   

    http://community.csdn.net/Expert/topic/3697/3697056.xml?temp=.1995813
      

  2.   

    定义一个Graphic类型的变量且该变量要用Assing()函数传递到相应数据库中保存。如: 
          procedureTForm1.btnSaveClick(sender:TObject); 
           var Graphic1: TGraphic; 
          begin 
         Graphic1 := TGraphic.Create; 
          Graphic1.LoadFromFile(OpenDialog1.Filename); 
          Table1.insert; 
         ... 插入其他字段 
          Table1.Fields[4].Assign(Graphic1); 
          Table1.Post; 
          Graphic1.Free; 
          end; 
        也可以这样将字段的内容保存: 
         Table1.Open; 
         Table1.Edit; 
         Table1.Fields[4].SaveToFile('C:\my.bmp'); 
         Table1.Close; 
        或这样读文件: 
         Table1.Open; 
         Table1.Edit; 
         Table1.Fields[4].LoadFromFile('c:\my.bmp'); 
         Table1.Close; 
     
        例如:存储图片的字段名是Picture,在窗体中放TQuery数据访问部件。 
        示范代码如下: 
        with Query1 do 
        begin 
         Close; 
         Sql.Clear; 
         Sql.Add('Insert into TableName(Picture)'); 
         Sql.Add('Values(:P_Picture)'); 
         ParamByName('P_Picture').LoadFromFile('FilePath'); 
         ExecSql; 
         Close; 
        end; 
        end; 
      

  3.   

    MyJPEG := TJPEGImage.Create;
       try
        with MyJPEG do
        begin
          Assign(Image1.Picture.Graphic);
          MS:=TMemoryStream.create;
          SaveToStream(MS);
          MS.Position:=0;
          TBlobField(FieldbyName('图片')).LoadFromStream(MS);
        end;
       finally
        MyJPEG.Free;
       end;
       try
       Post;
       except
       showmessage('数据无法提交');