Image类型的字段。
1、存文件入数据库
with Query do
begin
Close;
Sql.Clear;
Sql.Add('INSERT INTO Table VALUE(:sFileName,:sFileContent)');
ParamByName('sFileName').AsString := OpenDialog.FileName;
ParamByName('sFileContent').LoadFromFile(OpenDialog.FileName,ftBlob);
ExecSQL;
end;2、从数据库中取出文件
with Query do
begin
Close;
Sql.Clear;
Sql.Add('SELECT * FROM Table');
Open;
(FieldByName('sFileContent') AS TBlobField).SaveToFile (FieldByName('sFileName').AsString);
end;
1、存文件入数据库
with Query do
begin
Close;
Sql.Clear;
Sql.Add('INSERT INTO Table VALUE(:sFileName,:sFileContent)');
ParamByName('sFileName').AsString := OpenDialog.FileName;
ParamByName('sFileContent').LoadFromFile(OpenDialog.FileName,ftBlob);
ExecSQL;
end;2、从数据库中取出文件
with Query do
begin
Close;
Sql.Clear;
Sql.Add('SELECT * FROM Table');
Open;
(FieldByName('sFileContent') AS TBlobField).SaveToFile (FieldByName('sFileName').AsString);
end;
tAbs:TADOBlobStream;
tAbs:=TAdoBlobStream.create(TBlobField(FieldByName ('content')),bmRead)
你可以用OleContainer来显示:
tAbs.position:=0;
OleContainer1.LoadFromStream(tAbs);
OleContainer1.doVerb(ovShow);//显示文档
//保存时操作……
OleStream : TMemoryStream; OleStream := TMemoryStream.Create;
try
OleContainer1.SaveToStream(OleStream);
OleStream.Position := 0;
with Qry do
begin
Edit;
Tblobfield(FieldByName('Content')).LoadFromStream(OleStream);
Post;end
tAbs:TADOBlobStream;
tAbs:=TAdoBlobStream.create(TBlobField(FieldByName ('content')),bmRead)
你可以用OleContainer来显示:
tAbs.position:=0;
OleContainer1.LoadFromStream(tAbs);
OleContainer1.doVerb(ovShow);//显示文档
//保存时操作……
OleStream : TMemoryStream; OleStream := TMemoryStream.Create;
try
OleContainer1.SaveToStream(OleStream);
OleStream.Position := 0;
with Qry do
begin
Edit;
Tblobfield(FieldByName('Content')).LoadFromStream(OleStream);
Post;end
用你的方法可以存储任意文件,但是从数据库中取出文件后的文件好象只有图片,文本文件可以打开,其他的打开都有问题,存储可执行文件取出后不能执行。存储word文档取出后不能打开。
你的方法只限于文档吧!
如过你不用OleContainer 的话,取出时候不就是
tAbs:=TAdoBlobStream.create(TBlobField(FieldByName ('content')),bmRead)
吗?然后对tAbs操作!
var str:Tmemorystream;
begin
str:=Tmemorystream.Create;
str.LoadFromFile('d:\cai.jpg');//OpenDialog1.FileName 也可以
str.Position:=0; adoquery2.Append; //图片
Tblobfield(adoquery2.FieldByName('image')).loadfromstream(str);
try
adoquery2.Post;
finally
str.Free;
end;
end;
var str:Tmemorystream;
begin
str:=Tmemorystream.Create;
str.LoadFromFile('d:\cai.jpg');//OpenDialog1.FileName 也可以
str.Position:=0; adoquery2.Append; //图片
Tblobfield(adoquery2.FieldByName('image')).loadfromstream(str);
try
adoquery2.Post;
finally
str.Free;
end;
end;
Answer:
The main problem I faced when trying to do this was to deal with the fact that TField.Value returns a varOleStr no matter what was written into it, so the data needed to be converted into a more usable format. Note that there is no checking here that the TField is in fact of the correct type, and that the stream must be created and free-ed elsewhere manually. Also, additional memory equal to the size of the stream/blob is required, so be cautious if large amounts of data are involved. For ease of use in my own application, I incorporated this functionality into my descendent of TADOQuery.
function LoadFromBlob(const AField: TField; const Stream: TStream): boolean;
var
ResultStr: string;
PResultStr: PChar;
begin
Result := false;
if (Assigned(AField)) and (Assigned(Stream)) then begin
try
ResultStr := AField.Value;
PResultStr := PChar(ResultStr);
Stream.Write(PResultStr^, Length(ResultStr));
Stream.Seek(0,0);
Result := true;
except
end;
end;
end; function SaveToBlob(const Stream: TStream; const AField: TField): boolean;
var
FieldStr: string;
PFieldStr: PChar;
begin
Result := false;
if (Assigned(AField)) and (Assigned(Stream)) then begin
try
Stream.Seek(0,0);
SetLength(FieldStr, Stream.Size);
PFieldStr := PChar(FieldStr);
Stream.Read(PFieldStr^, Stream.Size);
AField.Value := FieldStr;
Result := true;
except
end;
end;
end; ------------------------------------------------------- Examples: If you have an ADO query "qryBlobTest" with the following fields: nFileIcon: Image; nFileData: Image; // Store an icon in an Image field
function StoreFileIcon: boolean;
var
AFileIcon: TIcon;
MS: TMemoryStream;
begin
Result := false;
AFileIcon := TIcon.Create;
MS := TMemoryStream.Create;
try
AFileIcon.handle := ExtractAssociatedIcon('c:\temp\Test.doc'); // Pseudocode !!
AFileIcon.SaveToStream(MS);
Result := SaveToBlob(MS, qryBlobTest.FieldByName('nFileIcon'));
finally
AFileIcon.Free;
MS.Free;
end;
end; // Load an icon from an Image field
function LoadFileIcon: boolean;
var
AFileIcon: TIcon;
MS: TMemoryStream;
begin
Result := false;
AFileIcon := TIcon.Create;
MS := TMemoryStream.Create;
try
if (LoadFromBlob(qryBlobTest.FieldByName('nFileIcon'), MS)) then begin
AFileIcon.LoadFromStream(MS);
// Do something with the Icon?
Result := true;
end;
finally
AFileIcon.Free;
MS.Free;
end;
end; // Save a binary file in an Image field
function StoreFileData: boolean;
var
FS: TFileStream;
begin
FS := TFileStream.Create('c:\temp\Test.doc', fmOpenRead);
Result := SaveToBlob(FS, qryBlobTest.FieldByName('nFileData'));
FS.Free;
end; // Load a file from an Image field (save it to a file name)
function LoadFileData: boolean;
var
FS: TFileStream;
begin
FS := TFileStream.Create('c:\temp\Test2.doc', fmCreate);
LoadFromBlob(qryBlobTest.FieldByName('nFileData'), FS);
FS.Free;
end;
var str:Tmemorystream; //修改图片POS_mgxid
begin
IF not ADOQUERY2.IsEmpty Then //如果更新编号不为空
Begin
str:=Tmemorystream.Create; //读取远程更新包的数据到程序执行的目录下面
str.Position:=0;
Tblobfield(adoquery2.FieldByName('image')).savetostream(str);
str.SaveToFile(ExtractFilePath(Application.ExeName)+'image\cai.jpg');
str.Free;
end;
如何将选取的文件存入数据库中的image字段这种方法是可以将任何文件存入数据。 但数据库每一字段里的数据必须是同一类型, 不可以一个字段列中存放不同类型的数据。