关于将文件存入数据库中的问题(300分)

  • 主题发起人 主题发起人 looksea
  • 开始时间 开始时间
L

looksea

Unregistered / Unconfirmed
GUEST, unregistred user!
请问如何将一个任意类型的文件存入数据库表中的某个字段?以及如何将其取出还原???
数据库为SQL7,如知道请提供详细 DELPHI原程序 THANKS ALL
 
读:
TBlobField * pField=(TBlobField *)pQuery->FieldByName("Image");
TBlobStream * pmem=new TBlobStream(pField,bmRead);
pmem->Seek(0,soFromBeginning);
Graphics::TBitmap * pBitmap=new Graphics::TBitmap();
pBitmap->LoadFromStream(pmem);
Image1->Picture->Assign(pBitmap);
delete pBitmap;
delete pmem;
写:
TBlobField * pField=(TBlobField *)pQuery->FieldByName("Image");
TBlobStream * pmem=new TBlobStream(pField,bmWrite);
pmem->Seek(0,soFromBeginning);
Graphics::TBitmap * pBitmap=new Graphics::TBitmap();
pBitmap->Assign(Image1->Picture->Graphic);
pBitmap->SaveToStream(pmem);
delete pBitmap;
delete pmem;

注意:一定要用此方法进行读写,因为实际上它是以二进制流的方式存入数据库!
 
一、存文件
procedure TForm1.Button1Click(Sender: TObject);
var str:Tmemorystream;
begin
str:=Tmemorystream.Create;
str.LoadFromFile('f:/page.pas');
str.Position:=0;
adoquery1.Append;
Tblobfield(adoquery1.FieldByName('tt')).loadfromstream(str);
try
adoquery1.Post;
finally
str.Free;
end;
二、取出还原
procedure TForm1.Button2Click(Sender: TObject);
var str:Tmemorystream;
begin
str:=Tmemorystream.Create;
str.Position:=0;
Tblobfield(adoquery1.FieldByName('tt')).savetostream(str);
str.SaveToFile('F:/gg.txt');
str.Free;
end;
 
多人接受答案了。
 
后退
顶部