用IMAGE类型。
给你一个例子。读与写,我是分成1M一个块保存的。
function TfrmResManager.ReadFileFromDataBase(
const FileName: string; const FileID: integer): Boolean;//把文件从数据库中读出再组合成文件
var FileStream: TFileStream; // 将写入的文件
BlobStream: TMemoryStream; //保存的BLOB流
iPosition: integer;
daqTemp: TADOQuery;
begin
Result := False;
FileStream := TFileStream.Create(FileName, fmCreate or fmOpenWrite);
BlobStream := TMemoryStream.Create;
daqTemp := dm.daqTemp;
try
with daqTemp do
begin
Active := False;
SQL.Text := 'select * from filesdata where i_id=' + IntToStr(FileID) + ' order by i_sort';
LockType := ltReadOnly;
Active := True;
if IsEmpty then
begin
MessageDlg('数据库中没有找到相应的文件!', mtError, [mbOK], 0);
abort;
end;
iPosition := 0;
while not Eof do
begin
Application.ProcessMessages;
BlobStream.Clear;
BlobStream.Position := 0;
TBlobField(FieldByName('vc_data')).SaveToStream(BlobStream);
FileStream.Seek(iPosition, soFromBeginning);
FileStream.CopyFrom(BlobStream,0);
iPosition := iPosition + BlobStream.Size;
Next;
end;
Active := False;
FileStream.Position := iPosition;
end;
finally
FileStream.Free;
BlobStream.Free;
end;
end;
function TfrmResManager.WriteFileToDataBase(
const FileName: string; const FileID: integer): Boolean;//把文件分解写入数据库
var FileStream: TFileStream; // 将写入的文件
BlobStream: TMemoryStream; //保存的BLOB流
iPosition,num: integer;
size: Cardinal;
begin
if not FileExists(FileName) then
begin
MessageDlg(FileName + '文件不存在!',mtError,[mbOK],0);
abort;
end;
FileStream := TFileStream.Create(FileName, fmOpenRead);
BlobStream := TMemoryStream.Create;
Size := FileStream.Size;
try
Num := 1;
iPosition := 0;
with daqFilesData do
begin
Active := True;
while iPosition < Size do
begin
Application.ProcessMessages;
Append;
FieldByName('i_id').AsInteger := FileID;
FieldByName('i_sort').AsInteger := Num;
FileStream.Position := iPosition;
BlobStream.Clear;
BlobStream.Position := 0;
if (iPosition + BlockSize) <= Size then
BlobStream.CopyFrom(FileStream, BlockSize)
else
BlobStream.CopyFrom(FileStream, Size - iPosition);
BlobStream.Position := 0;
TBlobField(FieldByName('vc_data')).LoadFromStream(BlobStream);
Post;
iPosition := iPosition + BlockSize;
inc(Num);
end;
Result := True;
Active := False;
end;
finally
FileStream.Free;
BlobStream.Free;
end;
end;
你试着改写,绝对没有问题的。我的软件中用得很好。