function SavetoImage(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;
下面是调用SavetoImage函数完成图像数据存储的程序片段。
Var
FS:TFileStream;
begin
FS:=TFileStream.Create('C:Car001.jpg',fmOpenRead);
SavetoImage(FS,Adodataset1.FieldBy
Name('st_img'));
FS.Free;
end;
var
s1:string;
begin
s1:='bcp "select * from st2002..st2002_sf where st_flag=1" queryout c:Media_data.dat -N -P -S sunnynthy2002';
winexec(PChar(s1),sw_show);
end;
其中“select * from st2002..st2002_sf where st_flag=1”表示从st2002数据库的st2002_sf表中提取数据,“c:Media_data.dat”为输出数据文件,参数 queryout表示从查询中复制数据到外部文件,-N表示进行大容量数据复制操作,-P表示使用默认密码,-S提定进行数据复制操作的数据库服务器或实 例。
var
s1:string;
begin
s1:= 'bcp ST2002..ST2002_SF in c:Media_data.dat -n -E -P -S sunnynt hy2002';
winexec(PChar(s1),sw_show);
end;
function LoadfromImage(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(Result
Str));
Stream.Seek(0,0);
Result:=True;
except
end;
end;
end;
var
FS:TFileStream;
begin
FS:=TFileStream.Create('c:Car001.jpg',fmCreate);
LoadfromImage(adodataset1.fieldby
name('st_img'),FS);
FS.Free;
image1.picture.LoadFromFile('c:Car001.jpg');
end;