程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> DELPHI ADO /ODAC 實現BLOB存取(文件大小不限制)

DELPHI ADO /ODAC 實現BLOB存取(文件大小不限制)

編輯:Delphi

 

通過ADO,ODAC實現文件上傳和讀取,具體代碼如下:

 

  源代碼下載鏈接:    http://download.csdn.net/download/xiechunlei8454/3875686

 

 

 

unit Unit1;

 

interface

 

uses

  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

  Dialogs, Grids, ValEdit, DB, ADODB, StdCtrls, DBAccess, Ora, MemDS,

  ExtCtrls, jpeg;

 

type

  TForm1 = class(TForm)

    adc: TADOConnection;

    os1: TOraSession;

    Edit1: TEdit;

    Label1: TLabel;

    Label2: TLabel;

    Edit2: TEdit;

    Button1: TButton;

    Button2: TButton;

    Button3: TButton;

    opd1: TOpenDialog;

    adq1: TADOQuery;

    oq1: TOraQuery;

    Button4: TButton;

    Button5: TButton;

    Label3: TLabel;

    Panel1: TPanel;

    img1: TImage;

    procedure Button1Click(Sender: TObject);

    procedure Button2Click(Sender: TObject);

    procedure Button3Click(Sender: TObject);

    procedure Button4Click(Sender: TObject);

    procedure Button5Click(Sender: TObject);

  private

    { Private declarations }

  public

    { Public declarations }

  end;

 

var

  Form1: TForm1;

 

implementation

 

{$R *.dfm}

 

procedure TForm1.Button1Click(Sender: TObject);

begin

  if opd1.Execute then begin

     edit2.Text:=opd1.FileName;

  end;

end;

 

procedure TForm1.Button2Click(Sender: TObject);

begin

  if edit1.Text='' then begin

     showmessage('Please Input Name First!');

     edit1.SelectAll;

     edit1.SetFocus;

     exit;

  end;

  if edit2.Text='' then begin

     showmessage('Please Choose File First!');

     button1.SetFocus;

     exit;

  end;

  with adq1 do begin

       close;

       sql.clear;

       sql.text:= 'select * from sfis1.c_test_t where name=:nm';

       Parameters.ParamByName('nm').Value:=edit1.Text;

       open;

       if RecordCount>0 then begin

          edit;

          fieldbyname('name').value:=edit1.Text;

          TBlobField(fieldbyname('appendex')).LoadFromFile(edit2.Text);

          post;

          close;

       end else begin

          append;

          fieldbyname('name').value:=edit1.Text;

          TBlobField(adq1.fieldbyname('appendex')).LoadFromFile(edit2.Text);

          post;

          close;

       end;

  end;

  edit1.Text:='';

  edit2.Text:='';

  showmessage('ADO SAVE OK!');

end;

 

procedure TForm1.Button3Click(Sender: TObject);

begin

  if edit1.Text='' then begin

     showmessage('Please Input Name First!');

     edit1.SelectAll;

     edit1.SetFocus;

     exit;

  end;

  if edit2.Text='' then begin

     showmessage('Please Choose File First!');

     button1.SetFocus;

     exit;

  end;

  with oq1 do begin

       close;

       sql.clear;

       sql.text:= 'select * from sfis1.c_test_t where name=:nm';

       parambyname('nm').AsString:=edit1.Text;

       open;

       if recordcount>0 then

          edit

       else

          append;

       fieldbyname('name').value:=edit1.Text;

       TBlobField(fieldbyname('appendex')).LoadFromFile(edit2.Text);

       post;

       close;

  end;

  edit1.Text:='';

  edit2.Text:='';

  showmessage('ODAC SAVE OK!');

end;

 

procedure TForm1.Button4Click(Sender: TObject);

var f1:string;

begin

  if edit1.Text='' then begin

     showmessage('Please Input Name First!');

     edit1.SelectAll;

     edit1.SetFocus;

     exit;

  end;

  if edit1.Text<>'' then begin

     with adq1 do begin

          close;

          sql.Clear;

          sql.Add('select * from sfis1.c_test_t where name=:nm');

          Parameters.ParamByName('nm').Value:=edit1.Text;

          Open;

          if recordcount>0 then begin

             f1:=extractfilepath(application.ExeName)+edit1.Text+'.JPG';

             if fileexists(f1) then

                deletefile(f1);

             Tblobfield(fieldbyname('appendex')).SaveToFile(f1);

             img1.Picture.LoadFromFile(f1);

             label3.Caption:='File Name Is: '+edit1.Text+'.JPG';

          end;

     end;

  end;

end;

 

procedure TForm1.Button5Click(Sender: TObject);

var f1:string;

begin

  if edit1.Text='' then begin

     showmessage('Please Input Name First!');

     edit1.SelectAll;

     edit1.SetFocus;

     exit;

  end;

  if edit1.Text<>'' then begin

     with oq1 do begin

          close;

          sql.Clear;

          sql.Add('select * from sfis1.c_test_t where name=:nm');

          ParamByName('nm').AsString:=edit1.Text;

          Open;

          if recordcount>0 then begin

             f1:=extractfilepath(application.ExeName)+edit1.Text+'.JPG';

             if fileexists(f1) then

                deletefile(f1);

             Tblobfield(fieldbyname('appendex')).SaveToFile(f1);

             img1.Picture.LoadFromFile(f1);

             label3.Caption:='File Name Is : '+edit1.Text+'.JPG';

          end;

     end;

  end;

 

end;

 

end.

 

摘自 xiechunlei8454的專欄

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved