我的控件中的一段代码, 存入流后,想存取文件就存取文件(TmemoryStream.savetofile loadfromfile), 想存取到数据库也是一样,动态记录。string类型等所有指针类数据结构都可以用我这个方法
type
TDataBaseType=(DT_standard,DT_Oracle,DT_DB2,DT_SQLSever,DT_Access,
DT_Sybase,DT_MySQL,DT_InterBase,DT_Informix,DT_Other,
DT_none);
byte256s= array of byte256;
R_BeskyDBLink=record
name:shortString;
DBType:TDataBaseType;
Linkstr:byte256s;
end;
const
MS_R_BeskyDBLink:integer=501;
Ms_Byte256s:integer=52;
function intSaveToStream(I:integer;ms:TMemoryStream):boolean;
function intLoadFromStream(var I:integer;ms:TMemoryStream):boolean;
function dataSaveToStream(bts:byte256s;ms:TMemoryStream):boolean;overload;
function dataLoadFromStream(var bts:byte256s;ms:TMemoryStream):boolean;overload;
function CopyRecord(Rs:R_BeskyDBLink;var RD:R_BeskyDBLink):boolean;overload;
function DisposeRecord(Var R:R_BeskyDBLink):boolean;overload;
function dataSaveToStream(R:R_BeskyDBLink;ms:TMemoryStream):boolean;overload;
function dataLoadFromStream(var R:R_BeskyDBLink;ms:TMemoryStream):boolean;overload;
//............
function intSaveToStream(I:integer;ms:TMemoryStream):boolean;
begin
result:=false;
if not assigned(ms) then exit;
try
ms.Write(i,sizeof(i));
result:=true;
except
on E : Exception do
Application.MessageBox(PansiChar(E.Message+'错误号:'+inttostr(E.HelpContext)),'错误!',MB_OK+MB_ICONERROR+MB_TOPMOST);
end;
end; //integer
function intLoadFromStream(var I:integer;ms:TMemoryStream):boolean;
begin
result:=false;
if not assigned(ms) then exit;
try
ms.Read(i,sizeof(i));
result:=true;
except
on E : Exception do Application.MessageBox(PansiChar(E.Message+'错误号:'+inttostr(E.HelpContext)),'错误!',MB_OK+MB_ICONERROR+MB_TOPMOST);
end;
end; //integer
function dataLoadFromStream(var bts:byte256s;ms:TMemoryStream):boolean;overload;
var
datatype:integer;
count,i:integer;
// sss:shortstrings;
begin
result:=false;
if not assigned(ms) then exit;
if not intloadfromStream(datatype,ms) then exit;
if datatype<>Ms_byte256s then exit;
if not intloadfromStream(count,ms) then exit;
if count=0 then exit;
setlength(bts,count);
try
for i:=0 to count-1 do ms.Read(bts,256);
result:=true;
except
on E : Exception do
Application.MessageBox(PansiChar(E.Message+'错误号:'+inttostr(E.HelpContext)),'错误!',MB_OK+MB_ICONERROR+MB_TOPMOST);
end;
end;
function dataSaveToStream(bts:byte256s;ms:TMemoryStream):boolean;overload;
var
i,count:integer;
begin
result:=false;
if not assigned(ms) then exit;
try
count:=high(bts)+1;
if not intSaveToStream(Ms_byte256s,ms) then exit;
if not intSaveToStream(count,ms) then exit;
if count=0 then exit;
for i:=0 to count-1 do
ms.Write(bts,256);
result:=true;
except
on E : Exception do
Application.MessageBox(PansiChar(E.Message+'错误号:'+inttostr(E.HelpContext)),
'错误!',MB_OK+MB_ICONERROR+MB_TOPMOST);
// messagedlg(E.Message,mtError ,[mbOK], E.HelpContext);
end;
end;
function CopyRecord(Rs:R_BeskyDBLink;var RD:R_BeskyDBLink):boolean;
begin
result:=false;
try
DisposeRecord(rd);
RD:=RS;
RD.Linkstr:=copy(RS.Linkstr);
result:=true;
except
on E: Exception do
Application.MessageBox(PansiChar(E.Message+'错误号:'+inttostr(E.HelpContext)),
'错误!',MB_OK+MB_ICONERROR+MB_TOPMOST);
end;
end;
function DisposeRecord(Var R:R_BeskyDBLink):boolean;
begin
result:=false;
try
setlength(r.Linkstr,0);
result:=true;
except
on E: Exception do
Application.MessageBox(PansiChar(E.Message+'错误号:'+inttostr(E.HelpContext)),
'错误!',MB_OK+MB_ICONERROR+MB_TOPMOST);
end;
end;
function dataSaveToStream(R:R_BeskyDBLink;ms:TMemoryStream):boolean;overload;
begin
result:=false;
if not assigned(ms) then exit;
try
if not intSaveToStream(Ms_R_BeskyDBLink,ms) then exit;
ms.Write(R.name,256);
ms.Write( R.DBType,sizeof( R.DBType));
dataSaveToStream(R.Linkstr,ms);
result:=true;
except
on E : Exception do
Application.MessageBox(PansiChar(E.Message+'错误号:'+inttostr(E.HelpContext)),
'错误!',MB_OK+MB_ICONERROR+MB_TOPMOST);
end;
end; // R_BeskyPanel
function dataLoadFromStream(var R:R_BeskyDBLink;ms:TMemoryStream):boolean;overload;
var
datatype:integer;
begin
result:=false;
if not assigned(ms) then exit;
try
if not intloadfromStream(datatype,ms) then exit;
if datatype<>Ms_R_BeskyDBLink then exit;
DisposeRecord(R);
ms.read(R.name,256);
ms.read( R.DBType,sizeof( R.DBType));
DataloadfromStream(R.Linkstr,ms);
result:=true;
except
on E : Exception do
Application.MessageBox(PansiChar(E.Message+'错误号:'+inttostr(E.HelpContext)),
'错误!',MB_OK+MB_ICONERROR+MB_TOPMOST);
end;
end;