将文件以二进制文件的形式存放到数据库中,需要时再将其导出来,首先看下实现代码:
方法1:直接以文件形式导出:
Procedure ExportAttachment(AdoConn: TADOConnection); var pAdoQry: TADOQuery; sSQL, sFileName: string; begin pAdoQry := TADOQuery.Create(nil); pAdoQry.Connection := AdoConn; try sSQL := 'SELECT * FROM TUXKNR'; if not OpenSQL(pAdoQry, sSQL) then Exit; // OpenSQL:公共函数 sFileName := 'D: mp mp.dwg'; if FileExists(sFileName) then DeleteFile(sFileName); if pAdoQry.FieldByName('TUNR').IsNull then Exit; TBlobField(pAdoQry.FieldByName('TUNR')).SaveToFile(sFileName); // TBlobField /r:DB finally FreeAndNil(pAdoQry); end; end;
方法2:以流的形式导出:
Procedure ExportAttachment(AdoConn: TADOConnection); var pAdoQry: TADOQuery; pStream: TMemoryStream; //r:classes pJpg: TJPEGImage; //r:jpeg sSQL: string; begin pAdoQry := TADOQuery.Create(nil); pAdoQry.Connection := AdoConn; try sSQL := 'SELECT * FROM TUXKNR'; if not OpenSQL(pAdoQry, sSQL) then Exit; // OpenSQL:公共函数 if pAdoQry.FieldByName('TUNR').IsNull then Exit; pStream:= TMemoryStream.Create; pJpg := TJPEGImage.Create; try TBlobField(pAdoQry.FieldByName('TUNR')).SaveToStream(pStream); pStream.Position := 0; pJpg.LoadFromStream(pStream); imgSign.Picture.Assign(pJpg); // imgSign:图片控件(为了显示效果) finally pJpg.Free; pStream.Free; end; finally FreeAndNil(pAdoQry); end; end;