Tabl1中有三个字段,其中Col3是文本字段或者Ole字段,当然,如果是备注字段,就不能保存二进制文件了.
代码实验通过。
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Db, ADODB, StdCtrls;
type
TForm1 = class(TForm)
ADOConnection1: TADOConnection;
aqry_Tmp: TADOQuery;
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
function DBToDisk(Query_String, Column_Name, Target_Name: string): Boolean;
function DiskToDB(l_Col1, Target_Name: string): Boolean;
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
function TForm1.DBToDisk(Query_String, Column_Name, Target_Name: string): Boolean;
var
Dot_Stream: TFileStream;
begin
{从数据库中取出文稿}
with aqry_Tmp do
begin
Close;
SQL.Text := Query_String;
Open;
end;
{将数据库中Image字段保存到硬盘下,存为Doc文件}
Dot_Stream := TFileStream.Create(Target_Name, fmOpenWrite or fmCreate);
Dot_Stream.Position := 0;
TBlobField(aqry_Tmp.FieldByName(Column_Name)).SaveToStream(Dot_Stream);
Dot_Stream.Free;
aqry_Tmp.Close;
Result := True; //所有任务彻底完成,Function返回True
end;
{-------------------------------------------------------------------------------}
{从硬盘文件读取数据存入数据库
Query_String 执行Query的SQL语句
l_Col1 提取的字段名
Target_Name 产生到硬盘的文件名(含完整路径)}
function TForm1.DiskToDB(l_Col1, Target_Name: string): Boolean;
var
Dot_Stream: TFileStream;
begin
with aqry_Tmp do
begin
Close;
SQL.Clear;
{里面仅仅有三个字段,将所有的都显示出来了}
SQL.Text := 'Select Col1,Col2,col3 from Table1 where col1=''' + l_Col1 + '''';
Open;
if IsEmpty = True then
begin
Result := False;
Exit;
end;
end;
aqry_Tmp.Edit; {直接进入Edit,取消Append}
Dot_Stream := TFileStream.Create(Target_Name, fmShareDenyNone);
Dot_Stream.Position := 0;
TBlobField(aqry_Tmp.FieldByName('col3')).loadfromStream(Dot_Stream);
aqry_Tmp.Post;
Dot_Stream.Free;
Result := True; //一切任务完成,返回True
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
DiskToDB('a', 'E:/U100000000562.txt');
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
DBToDisk('Select * From Table1 Where Col1=''a''', 'Col3', 'E:/a.txt');
end;
end.