Delphi 如何将二维数组数据 以Blob 存入数据库中 并读取
var
TwoMatrix,TwoMatrixRet:array[0..39] of array[0..39] of Int16;
Row: Integer;
Col: Integer;
ms:TMemoryStream;
ms1:TStream;
len:integer;
I: Integer;
begin
for Row := 0 to 40 - 1 do
begin
for Col := 0 to 40 - 1 do
begin
TwoMatrix[Row][Col]:=Random(255);
TwoMatrixRet[Row][Col]:=0;
end;
end;
ms:=TMemoryStream.Create;
ms.SetSize(40*40*2);
Move(TwoMatrix[0][0],ms.Memory^,40*40*2);
//1、尝试连接数据库
if UniConnectionTB.Connected = false then //
begin
UniConnectionTB.Connect;
end;
//2、写入 更新数据
UniQueryTB.Close;
UniQueryTB.SQL.Clear;
UniQueryTB.SQL.Add('update twomatrixtable set ID=:ID, TwoMatrix=:TwoMatrix ');
UniQueryTB.SQL.Add('where (ID=:A_ID) ');
UniQueryTB.ParamByName('ID').value := 1;
UniQueryTB.ParamByName('TwoMatrix').LoadFromStream(ms,ftBlob);
UniQueryTB.ParamByName('A_ID').value := 1;
UniQueryTB.ExecSQL;
//3、读出来
UniQueryTB.Close;
UniQueryTB.SQL.Clear;
UniQueryTB.SQL.Add('select *from twomatrixtable where ID =1 ');
UniQueryTB.Open;
ms1:= UniQueryTB.CreateBlobStream( UniQueryTB.FieldByName('TwoMatrix'),TBlobStreamMode.bmRead);
Len := ms1.Size;
MS1.Read(TwoMatrixRet,len);
end.