摘要:檔案存入資料庫
環境Delphi6 + MSSQL2008
使用者需求剛好隨手紀錄一下
設計介面
程式碼
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, DB, ADODB, StdCtrls, Buttons, ExtCtrls, DBCtrls, Grids, DBGrids; type TForm1 = class(TForm) Button1: TButton; Edit1: TEdit; ADOConnection1: TADOConnection; ADODataSet1: TADODataSet; ADODataSet1cid: TAutoIncField; ADODataSet1filename: TWideStringField; ADODataSet1filebit: TBlobField; Button2: TButton; DataSource1: TDataSource; DBText1: TDBText; Label1: TLabel; Edit2: TEdit; Button4: TButton; DBNavigator1: TDBNavigator; DBGrid1: TDBGrid; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure Button4Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; ss:string; blob,fs: TStream; implementation {$R *.dfm} procedure TForm1.Button1Click(Sender: TObject); VAR searchrec_om: TSearchrec; exeFilestream:Tfilestream; begin if (edit1.Text='') then MessageBox(Handle,pchar('文件路徑有誤!'),'提示',MB_ICONEXCLAMATION) else begin ss:=edit1.Text; blob := ADODataSet1.CreateBlobStream(ADODataSet1.FieldByName('filebit'), bmWrite); try blob.Seek(0, soFromBeginning); if FileExists(edit1.Text) then fs := TFileStream.Create(ss, fmOpenRead ); try blob.CopyFrom(fs, fs.Size) finally fs.Free end; finally blob.Free ; end; ADODataSet1filename.AsString:=EDIT2.Text; ADODataSet1.Post; end; end; procedure TForm1.Button2Click(Sender: TObject); var filehandle: integer; OpenDialog2: TOpenDialog; begin ADODataSet1.Open; ADODataSet1.edit; OpenDialog2 := TOpenDialog.Create(self); if OpenDialog2.Execute then begin edit1.text:= OpenDialog2.FileName; edit2.text:=ExtractFileName(OpenDialog2.FileName); //檔名 // edit2.text:=ExtractFilePath(Opendialog1.ExeName); //路徑 end; // OpenDialog2.Destroy; // OpenDialog2.Free; end; procedure TForm1.Button4Click(Sender: TObject); var SaveDialog1:TSaveDialog; begin SaveDialog1:=TSaveDialog.Create(self); blob := ADODataSet1.CreateBlobStream(ADODataSet1.FieldByName('filebit'), bmRead); try blob.Seek(0, soFromBeginning); if SaveDialog1.Execute then with TFileStream.Create(SaveDialog1.filename, fmCreate) do try CopyFrom(blob, blob.Size) finally Free end; finally blob.Free end; end; end.
參考http://www.scalabium.com/faq/dct0141.htm
大家一起加入blogads 賺零用錢!!