//转来的,以前的答案中的
提供以下例程,仅供参考:
unit blobFun;
////////////////////////////////////////////////////////////////////
//有关blob字段的读写操作公共函数
////////////////////////////////////////////////////////////////////
interface
uses classes,db,dbtables,graphics;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//功能:IMAGE字段的读写操作
function gfWriteDataToBlobField(bIndex,eIndex:integer;data:array of double;var fieldObject:TBlobField):boolean;
function gfReadDataFromBlobField(bIndex,eIndex:integer;var data:array of double;fieldObject:TBlobField):boolean;
function gfWriteImageToBlobField(bitmapObject:TBitmap;var fieldObject:TBlobField):boolean;
function gfReadImageFromBlobField(var bitmapObject:TBitmap;fieldObject:TBlobField):boolean;
implementation
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//功能:从IMAGE字段中读取数据到数组中
//输入: data:实型数组 bIndex,eIndex:数组起止索引 fieldObject:与IMAGE字段对应的fieldObject对象
//输出:fieldObject对象数据将被修改 返回成功标志TRUE
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function gfWriteDataToBlobField(bIndex,eIndex:integer;data:array of double;var fieldObject:TBlobField):boolean;
var
bufferLen:integer;
dataStream:TMemoryStream;
begin
//check parameters:
// Assert((data<>nil) AND (fieldObject<>nil),'Parameters error in gfReadDataFromBlobField()!');
Assert(eIndex>=bIndex,'Parameters error in gfReadDataFromBlobField()!');
Result:=true;
//make memory stream
bufferLen:=sizeof(double)*(eIndex-bIndex);
dataStream:=TMemoryStream.Create;
dataStream.SetSize(bufferLen+1);
//write data into memory stream
dataStream.Write(data,bufferlen);
//write data into field object from memory stream
try
fieldObject.LoadFromStream(dataStream);
except
Result:=False;
end;
dataStream.free;
end;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//功能:从IMAGE字段中读取数据到数组中
//输入: data:实型数组 bIndex,eIndex:数组起止索引 fieldObject:与IMAGE字段对应的fieldObject对象
//输出:data数组中数据将被修改 返回成功标志TRUE
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function gfReadDataFromBlobField(bIndex,eIndex:integer;var data:array of double;fieldObject:TBlobField):boolean;
var
bufferLen:integer;
dataStream:TMemoryStream;
myBlobStream:TBlobStream;
begin
//check parameters:
// Assert((data<>nil) AND (fieldObject<>nil),'Parameters error in gfReadDataFromBlobField()!');
Assert(eIndex>=bIndex,'Parameters error in gfReadDataFromBlobField()!');
Result:=true;
//make memory stream
bufferLen:=sizeof(double)*(eIndex-bIndex);
dataStream:=TMemoryStream.Create;
dataStream.SetSize(bufferLen+1);
try
begin
//make blob stream
myBlobStream:=TBlobStream.Create(fieldObject,bmRead);
//read data from blob stream to memory stream
dataStream.LoadFromStream(myBlobStream);
end
except
begin
Result:=false;
end
end;
//read data into data array from memory stream
if Result then dataStream.Read(data[bIndex],bufferlen);
dataStream.Free;
end;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//功能:从IMAGE字段中读取数据到image对象中
//输入: bitmapObject:图形对象 fieldObject:与IMAGE字段对应的fieldObject对象
//输出:fieldObject数据将被修改 返回成功标志TRUE
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function gfWriteImageToBlobField(bitmapObject:TBitmap;var fieldObject:TBlobField):boolean;
var
bufferLen:integer;
dataStream:TMemoryStream;
begin
//check parameters:
Assert(((bitmapObject<>nil) AND (fieldObject<>nil)),'Parameters error in gfReadDataFromBlobField()!');
Result:=true;
//make memory stream
bufferLen:=sizeof(bitmapObject);
dataStream:=TMemoryStream.Create;
dataStream.SetSize(bufferLen+1);
//write data into memory stream
bitmapObject.SaveToStream(dataStream);
//write data into field object from memory stream
try
fieldObject.LoadFromStream(dataStream);
except
Result:=False;
end;
dataStream.free;
end;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//功能:从IMAGE字段中读取数据到image对象中
//输入: bitmapObject:图形对象 fieldObject:与IMAGE字段对应的fieldObject对象
//输出: bitmapObject数据将被修改 返回成功标志TRUE
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function gfReadImageFromBlobField(var bitmapObject:TBitmap;fieldObject:TBlobField):boolean;
var
myBlobStream:TBlobStream;
begin
//check parameters:
Assert((bitmapObject<>nil) AND (fieldObject<>nil),'Parameters error in gfReadDataFromBlobField()!');
Result:=true;
//make memory stream
try
begin
//make blob stream
myBlobStream:=TBlobStream.Create(fieldObject,bmRead);
//read data from blob stream to memory stream
bitmapObject.LoadFromStream(myBlobStream);
end
except
begin
Result:=false;
end
end;
//read data into data array from memory stream
myBlobStream.Free;
end;
end.