Delphi:TMemoryStream类--二进制大对象与数据库的交互
2006-04-19 16:18
357 查看
Q:如何将一个大的二进制数组保存到数据库,并从数据库再次取回到数组?
A:使用TMemoryStream类对象,使用内存流对象的WriteBuffer/ReadBuffer方法和数据库BLOB字段对象的SaveToStream/WriteFromStream方法。以下是我做的一个例子,Form1只有一个按钮,但是要uses DataModule2;DataModule2里包含一个ADOTable1,指向一个只有两个字段的表,一个字段是序号索引(可以让Access自动生成),另一个字段是BLOB类型,记得要对这个表生成字段域变量(右键点击ADOTable1-->Fiels Editor...)。
procedure TForm1.Button1Click(Sender: TObject);
var
SourceString: Array[0..1023] of byte;//byte数组
DesString: Array[0..1023] of byte;//byte数组
MemoryStream,MemoryStream2 : TMemoryStream;
i:integer;
begin
for i:=Low(SourceString) to High(SourceString) do
begin
SourceString[i]:=i;
end;
MemoryStream := TMemoryStream.Create;
MemoryStream2:= TMemoryStream.Create;
try
// Write the string to the stream. We want to write from SourceString's
// data, starting at a pointer to SourceString (this returns a pointer to
// the first character), dereferenced (this gives the actual character).
//MemoryStream.WriteBuffer(Pointer(SourceString)^, Length(SourceString));//String写到内存流
MemoryStream.WriteBuffer(SourceString, Length(SourceString));//String写到内存流
MemoryStream.Position := 0; // Go back to the start of the stream
//SetLength(SourceString, MemoryStream.Size); // Set the length, so we have space to read into
MemoryStream.ReadBuffer(DesString, MemoryStream.Size);//读内存流到字符串 Read it back in to make sure it worked.
for i:=0 to Length(DesString)-1 do
Caption :=Caption+','+ inttostr(DesString[i]);
//ok,from here we can use MemoryStream to save into DB using LoadFromStream Method.
//使用
DataModule2.ADOTable1.Open;
DataModule2.ADOTable1.Edit;
DataModule2.ADOTable1.Append;
//从内存流到数据库ADOTable1BinaryData represent a Table's Blob field.
DataModule2.ADOTable1BinaryData.LoadFromStream(MemoryStream);
DataModule2.ADOTable1.First;
DataModule2.ADOTable1BinaryData.SaveToStream(MemoryStream2);//从数据库到内存流2
MemoryStream2.Position :=0;
MemoryStream2.ReadBuffer(DesString, MemoryStream2.Size); //从内存流2到 Byte数组DesString
Label1.Caption :='';
for i:=0 to Length(DesString)-1 do
//inttostr:Byte不是 integer,但是我不知道有没有Bytetostr这样的函数,先就凑合着inttostr用吧。
Label1.Caption :=Label1.caption+','+ inttostr(DesString[i]);
finally
MemoryStream.Free;
MemoryStream2.Free;
end;
end;
A:使用TMemoryStream类对象,使用内存流对象的WriteBuffer/ReadBuffer方法和数据库BLOB字段对象的SaveToStream/WriteFromStream方法。以下是我做的一个例子,Form1只有一个按钮,但是要uses DataModule2;DataModule2里包含一个ADOTable1,指向一个只有两个字段的表,一个字段是序号索引(可以让Access自动生成),另一个字段是BLOB类型,记得要对这个表生成字段域变量(右键点击ADOTable1-->Fiels Editor...)。
procedure TForm1.Button1Click(Sender: TObject);
var
SourceString: Array[0..1023] of byte;//byte数组
DesString: Array[0..1023] of byte;//byte数组
MemoryStream,MemoryStream2 : TMemoryStream;
i:integer;
begin
for i:=Low(SourceString) to High(SourceString) do
begin
SourceString[i]:=i;
end;
MemoryStream := TMemoryStream.Create;
MemoryStream2:= TMemoryStream.Create;
try
// Write the string to the stream. We want to write from SourceString's
// data, starting at a pointer to SourceString (this returns a pointer to
// the first character), dereferenced (this gives the actual character).
//MemoryStream.WriteBuffer(Pointer(SourceString)^, Length(SourceString));//String写到内存流
MemoryStream.WriteBuffer(SourceString, Length(SourceString));//String写到内存流
MemoryStream.Position := 0; // Go back to the start of the stream
//SetLength(SourceString, MemoryStream.Size); // Set the length, so we have space to read into
MemoryStream.ReadBuffer(DesString, MemoryStream.Size);//读内存流到字符串 Read it back in to make sure it worked.
for i:=0 to Length(DesString)-1 do
Caption :=Caption+','+ inttostr(DesString[i]);
//ok,from here we can use MemoryStream to save into DB using LoadFromStream Method.
//使用
DataModule2.ADOTable1.Open;
DataModule2.ADOTable1.Edit;
DataModule2.ADOTable1.Append;
//从内存流到数据库ADOTable1BinaryData represent a Table's Blob field.
DataModule2.ADOTable1BinaryData.LoadFromStream(MemoryStream);
DataModule2.ADOTable1.First;
DataModule2.ADOTable1BinaryData.SaveToStream(MemoryStream2);//从数据库到内存流2
MemoryStream2.Position :=0;
MemoryStream2.ReadBuffer(DesString, MemoryStream2.Size); //从内存流2到 Byte数组DesString
Label1.Caption :='';
for i:=0 to Length(DesString)-1 do
//inttostr:Byte不是 integer,但是我不知道有没有Bytetostr这样的函数,先就凑合着inttostr用吧。
Label1.Caption :=Label1.caption+','+ inttostr(DesString[i]);
finally
MemoryStream.Free;
MemoryStream2.Free;
end;
end;
相关文章推荐
- Delphi:TMemoryStream类--二进制大对象与数据库的交互
- 数据库交互的几个简单的对象
- 保存二进制对象到数据库
- Oracle数据库对于大对象二进制文件的存储及下载
- 用VC,VB进行图像数据(二进制大对象)存储数据库的方法
- Delphi用Webservice传输二进制对象
- SharePoint 2013 使用 RBS 功能将二进制大型对象 BLOB 存储在内容数据库外部。
- 关于用VC,VB进行图像数据(二进制大对象)存储数据库的一点心
- 在DELPHI程序中使用ADO对象存取ODBC数据库
- 二进制图片、二进制pdf 简单的数据库和客户端的交互
- image控件与数据库中二进制图片的交互(存储与读取)
- 在DELPHI程序中使用ADO对象存取ODBC数据库
- 以二进制方式存储对象到数据库
- SharePoint 2013 使用 RBS 功能将二进制大型对象 BLOB 存储在内容数据库外部。
- hibernate crud的两种方案对象和sql ---- session和sql与数据库交互
- 关于用VC,VB进行图像数据(二进制大对象)存储数据库的一点心得
- AO中保存二进制大对象(BLOB)-----将图层保存到数据库中
- 在DELPHI程序中使用ADO对象存取ODBC数据库
- Delphi用Webservice传输二进制对象