我想做个通讯录,里面有一步是给某个人添加照片,就是点击一个speedbuttton, 出现选择图片,然后将图片显示出来,再添加到数据库里,如何才能实现啊,我用的是access数据库

解决方案 »

  1.   

    用一个如Photo这样一个字段,然后设置类型为OLE对象类型,然后在访问时可以用TBlobField(Query1.FieldByName('Photo')).LoadFromFile('xxxxx\xxx.bmp');即可
      

  2.   

    procedure savepicture(save:boolean;filepath:string;Number:string;fieldname:string) ;
    var myfilestream:tfilestream;
        myblob:tblobfield;
    begin
    if save then
     begin
       with datam.ADOQNew do
         begin
           close;
           sql.Clear;
           sql.Add('select * from table');
           open;
           edit;
           myfilestream:=tfilestream.Create(filepath,fmopenread);
           myblob:=tblobfield(FieldByName(fieldname));
           myblob.LoadFromStream(myfilestream);
           myfilestream.Free;
           post;
         end;
      end;
    end;
      

  3.   

    有源码。
    unit Unit1; 
    interface 
    uses
    Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
    ExtCtrls, DBCtrls, Grids, DBGrids, Db, ADODB,jpeg, StdCtrls,dbtables;
    {一定要USES JPEG单元,使能存储JPG文件格式}
    type
    TForm1 = class(TForm)
    DataSource1: TDataSource;
    ADOQuery1: TADOQuery;
    DBGrid1: TDBGrid;
    DBNavigator1: TDBNavigator;
    Image1: TImage;
    savebutton: TButton;
    showbutton: TButton;
    OpenDialog1: TOpenDialog;
    ADOQuery1id: TIntegerField;
    ADOQuery1pic: TBlobField;
    procedure savebuttonClick(Sender: TObject);
    procedure showbuttonClick(Sender: TObject);
    procedure DBNavigator1Click(Sender: TObject; Button: TNavigateBtn);
    private
    { Private declarations }
    public
    { Public declarations }
    end;var
    Form1: TForm1;implementation{$R *.DFM}function JpegStartsInBlob(PicField:TBlobField):integer;
    var
    ghy: TADOBlobstream;
    buffer:Word;
    hx: string;
    begin
    Result := -1;
    ghy := TADOBlobstream.Create(PicField, bmRead);
    try
    while (Result = -1) and (ghy.Position + 1 < ghy.Size) do
    begin
    ghy.ReadBuffer(buffer, 1);
    hx:=IntToHex(buffer, 2);
    if hx = 'FF' then begin
    ghy.ReadBuffer(buffer, 1);
    hx:=IntToHex(buffer, 2);
    if hx = 'D8' then Result := ghy.Position - 2
    else if hx = 'FF' then
    ghy.Position := ghy.Position-1;
    end; //if
    end; //while
    finally
    ghy.Free
    end; //try
    end;
    procedure TForm1.savebuttonClick(Sender: TObject);
    var
    picstream:tadoblobstream;
    begin
    adoquery1.edit;
    picstream:=tadoblobstream.Create(tblobfield(adoquery1.fields[1]),bmWrite);
    if form1.opendialog1.execute then
    begin
    picstream.LoadFromFile(opendialog1.filename);
    picstream.Position:=0;
    adoquery1.edit;
    tblobfield(adoquery1.Fields[1]).loadfromstream(picstream);
    adoquery1.post;
    end;
    end;procedure TForm1.showbuttonClick(Sender: TObject);
    var
    ghy:TADOBlobstream;
    pic:tjpegimage;
    begin
    ghy := TADOBlobstream.Create(Adoquery1pic, bmRead);
    try
    ghy.Seek(JpegStartsInBlob(Adoquery1pic),soFromBeginning);
    Pic:=TJpegImage.Create;
    try
    Pic.LoadFromStream(ghy);
    Image1.Picture.Graphic:=Pic;
    finally
    Pic.Free;
    end;
    finally
    ghy.Free
    end;
    end;procedure TForm1.DBNavigator1Click(Sender: TObject; Button: TNavigateBtn);
    begin
    if button in [nbFirst, nbPrior, nbNext, nbLast] then showbutton.Click;
    end;end.如果数据库中要存储的是BMP文件,则在procedure TForm1.showbuttonClick(Sender: TObject);过程中代码更改如下即可存储显示BMP文件格式的操作。
    procedure TForm1.showbuttonClick(Sender: TObject);
    var
    ghy:TADOBlobstream;
    pic:tbitmap;
    begin
    ghy := TADOBlobstream.Create(Adoquery1pic, bmRead);
    try
    { ghy.Seek(JpegStartsInBlob(Adoquery1pic),soFromBeginning);}
    Pic:=Tbitmap.Create;
    try
    Pic.LoadFromStream(ghy);
    Image1.Picture.Graphic:=Pic;
    finally
    Pic.Free;
    end;
    finally
    ghy.Free
    end;
    end;
      到此用DELPHI存取JPEG文件到SQL Server数据库中的具体操作已经叙述完毕。
      

  4.   

    这种方法同样也适用于三层分布式结构
    function Tform1.PicToVarArray(pic: TPicture): Variant;
    var
      MS: TMemoryStream;
      s: string;
    begin
      MS := TMemoryStream.Create();
      Pic.Bitmap.SaveToStream(MS);
      SetLength(S, MS.Size);
      Move(MS.Memory^, Pointer(S)^, MS.Size);
        MS.Free();
      Result := StringToVariantArray(S);
    end;function saveP(const a1: WideString;
      var imag: OleVariant): WideString;
    var
     sql string;
    begin
    sql:='insert (a1,pic) values(''+a1+','+imag)
     adoquery1.close
    adoquery1.sql.text:=sql;
    adoquery1.exec;
    end;