我用ORACLE 数据库,语言用DELPHI6.0
我用三层结构,请问我要把图片保存到数据库中,客户端我用clentdataset,请问各位老师怎么实现啊。
谢谢,我在线等后。

解决方案 »

  1.   

    图片的字段用BLOB
    图片存储问题.unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,jpeg,
      ExtDlgs, Db, DBTables, StdCtrls, ExtCtrls, ADODB;type
      TForm1 = class(TForm)
        Button1: TButton;
        OpenPictureDialog1: TOpenPictureDialog;
        Button2: TButton;
        Image1: TImage;
        ADOQuery1: TADOQuery;
        ADOQuery2: TADOQuery;
        Edit1: TEdit;
        procedure Button1Click(Sender: TObject);
        procedure Button2Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
        filename:string;
        procedure SetPicture(pos:string;TempQuery:TADOQuery);     /////往数据库中插入图片
        procedure GetPicture(pos:string;TempQuery:TADOQuery);      ////得到图片
      end;var
      Form1: TForm1;implementation{$R *.DFM}{ TForm1 }procedure TForm1.GetPicture(pos:string;TempQuery: TADOQuery);
    var
      MS_JpegStream:TMemoryStream;
    begin
      try
        MS_JpegStream:=TMemoryStream.Create;
        with tempQuery do
          begin
            close;
            sql.clear;
            //修改这个语句
            sql.Add('select picture from table where no=:pos');
            Parameters.ParamByName('pos').value:=pos;
            Open;
          end;
        if tempquery.FieldByName('image').isnull then
          begin
            image1.Picture.Graphic:=nil;
          end
        else
          begin
             (tempQuery.FieldByName('image') as tblobfield).savetostream(MS_JpegStream);
             image1.Picture.Graphic:=nil;
             image1.Picture.Graphic:=TJpegImage.Create;
             MS_JpegStream.Position:=0;
             image1.Picture.Graphic.LoadFromStream(MS_JpegStream);
          end;  finally
        MS_JpegStream.Free;
      end;end;procedure TForm1.SetPicture(pos:string; TempQuery: TADOQuery);
    var
      MS_JpegStream:TMemoryStream;
      M_BitMap:TBitMap;
      M_Jpeg:TJpegImage;
    begin
      try
        MS_JpegStream:=TMemoryStream.Create;
        M_BitMap:=TBitMap.Create;
        M_Jpeg:=TJpegImage.Create;
        if extractfileext(filename)='.bmp' then
          begin
            M_BitMap.LoadFromFile(filename);
            M_Jpeg.Compress;
            M_Jpeg.Assign(M_BitMap);
          end
        else if extractfileext(filename)='.jpg' then
          begin
            M_Jpeg.LoadFromFile(filename);
          end;
        M_Jpeg.SaveToStream(MS_JpegStream);
        with tempQuery do
          begin
            close;
            sql.clear;
    //修改这个语句
            sql.Add('insert into table (no,IMage) values (:pos,:image)');
            Parameters.ParamByName('pos').Value:=pos;
            Parameters.ParamByName('image').LoadFromStream(MS_JpegStream,ftblob);        ExecSQL;
          end;
      finally
        MS_JpegStream.Free;
        M_BitMap.Free;
        M_Jpeg.Free;
      end;
    end;procedure TForm1.Button1Click(Sender: TObject);//打开图片并写入数据库
    begin
      if openpicturedialog1.Execute then
      begin
       filename:=openpicturedialog1.FileName;
       SetPicture(edit1.text,ADOQuery1);
      end
    end;procedure TForm1.Button2Click(Sender: TObject);//显示图片
    begin
      getpicture(edit1.text,ADOQuery2);
    end;end.
      

  2.   

    image字段
    var
      strm : Tmemorystream;
    begin
      if fileexists(openpicturedialog1.FileName) then
      begin
      strm := Tmemorystream.Create;
      strm.LoadFromFile(openpicturedialog1.FileName);
      strm.Position := 0;
      with dm.ClassMateQuery do
      begin
        append;
        TBlobfield(fieldbyname('pic')).LoadFromStream(strm);   
        post; 
      end;
      strm.Free;
      end;
      

  3.   

    TO:houwei1008(下雨天) 
    有问题,编译时出错:Undeclared identifier:ftblob
                        Undeclared identifier:Tblobfield
                        Undeclared identifier:savetostream
      

  4.   

    添加:procedure Tstudent_Form.Button1Click(Sender: TObject);
    var strm:Tmemorystream;
        jimg:Tjpegimage;
    begin
      with d_module.student_Query do
        begin
          close;
          sql.Clear;
          sql.Add('select uid from user_info where study_no='''+edit1.Text+'''');
          open;  //检查数据表中是否存在同样的记录
          if eof and bof then
            begin
               close;
               strm:=Tmemorystream.Create;
              // image1.Picture.Graphic.SaveToStream(strm);
               jimg:=Tjpegimage.Create;
               if filename='' then filename:='default.jpg';
               jimg.LoadFromFile(filename);
               jimg.SaveToStream(strm);
               strm.Position:=0;
               sql.Clear;
               sql.Add('insert into user_info (study_no,uname,class_no,sex,fathername,');
               sql.Add('fathermobile,mothername,mothermobile,photo) values ('''+edit1.Text+'''');
               sql.Add(','''+edit2.Text+''','''+combobox1.Text+''','''+combobox2.Text+''','''+edit3.Text+''',');
               sql.Add(''''+edit4.Text+''','''+edit5.Text+''','''+edit6.Text+''',:a)');
               parambyname('a').LoadFromStream(strm,ftblob);
               //showmessage(sql.Text);
               execsql;
               jimg.Free;
               strm.Free;
               label10.Caption:='添加成功!';
               edit1.Text:='';
               edit2.Text:='';
               edit3.Text:='';
               edit4.Text:='';
               edit5.Text:='';
               edit6.Text:='';
               combobox1.ItemIndex:=-1;
               combobox2.ItemIndex:=-1;
            end
            else
             begin
                 showmessage('对不起,该学号的学员信息已存在!');
                 exit;
             end;
        end;
        d_module.search_student_Query.close;
        d_module.search_student_Query.open;
    end;读取:procedure Tstudent_Form.N1Click(Sender: TObject);
    var jpgimg:tblobstream;
        jpg:Tjpegimage;
    begin
        button3.Enabled:=true;
       edit7.Text:=dbgrid1.SelectedField.AsString;
      // showmessage(edit7.Text);
       with d_module.student_Query do
         begin
             close;
             sql.Clear;
             sql.Add('select * from user_info where uid='+edit7.Text);
           //  showmessage(sql.Text);
             open;
             edit1.Text:=fieldbyname('study_no').AsString;
             edit2.Text:=fieldbyname('uname').AsString;
             //combobox1.Text:=fieldbyname('class_no').AsString;
             combobox1.ItemIndex:=combobox1.Items.IndexOf(fieldbyname('class_no').AsString);
             combobox2.ItemIndex:=combobox2.Items.IndexOf(fieldbyname('sex').AsString);
             edit3.Text:=fieldbyname('fathername').AsString;
             edit4.Text:=fieldbyname('fathermobile').AsString;
             edit5.Text:=fieldbyname('mothername').asstring;
             edit6.Text:=fieldbyname('mothermobile').asstring;
             if fieldbyname('photo').IsNull then
                image1.Picture.LoadFromFile('AC_ba.jpg')
             else
               begin
                  jpgimg:=tblobstream.Create(tblobfield(fieldbyname('photo')),bmread);
                 // Tblobfield(fieldbyname('photo')).savetostream(jpgimg);
                  jpgimg.Position:=0;
                  image1.Picture.Graphic:=nil;
                  jpg:=Tjpegimage.Create;
                  jpg.LoadFromStream(jpgimg);
                  image1.Picture.Graphic:=jpg;
                  jpg.Free;
                  jpgimg.Free;
               end;
             close;
         end;   // combobox1.Items.
    end;注意在uses中引用:
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, Grids,Db, DBTables, DBGrids, Menus,Jpeg, ExtCtrls, ExtDlgs,ADODB;没错的,我现在就这么写的,但是当图片太大时,有点问题,有部分图片显示不出来,应该是没有存进去。
      

  5.   

    说明:filename是定义的一个全局变量,通过openpicturedialog取值的。