如何将variant类型转换成record类型?(50分)

  • 主题发起人 主题发起人 landina
  • 开始时间 开始时间
L

landina

Unregistered / Unconfirmed
GUEST, unregistred user!
有没有通用的函数?
 
你没说明白
 
recordset可以用variant代的。
 
procedure TTollStateFrm.btnFindClick(Sender: TObject);
var
FVTypeID,FVID,TypeId,ID:string;
Data:OleVariant;
cls,clsTmp:Tclientdataset;
begin
TollInfoID:=0;
if CheckData then
begin
if not clsdata.IsEmpty then while not clsdata.Eof do clsdata.Delete;
clsTmp:=Tclientdataset.Create(nil);
cls:=Tclientdataset.Create(nil);
FVTypeID:=ParentFrm.rmtTollOperation.fieldbyname('FVTypeID').AsString;
FVID:=edtFVID.Text;
QryObj.GetRecords(gCurUser.SecurityID,'select * from TollDetail where FVTypeID='''+ FVTypeID+''' and FVID='''+ FVID+'''',data);
clsTmp.Data:=Data;
if clsTmp.RecordCount<=0 then
....
 
program Project1;
{$APPTYPE CONSOLE}
uses SysUtils;

type
TMan = record
Name: string;
Age: byte;
end;

procedure RecordToVariant(var ARec; Size: integer; var v: Variant);
var
p: PByteArray;
prec: Pointer;
i: integer;
begin
v := VarArrayCreate([0, Size -1], varByte);
p := VarArrayLock(v);
prec := @ARec;
for i:=0 to Size-1 do
p^ := (PByteArray(prec))^;
VarArrayUnLock(v);
end;

procedure VariantToRecord(v: Variant; var ARec; Size: integer);
var
i: integer;
p: PByteArray;
prec: Pointer;
begin
prec:=@ARec;
p := VarArrayLock(v);
for i:=0 to Size-1 do
(PByteArray(prec))^ := p^;
VarArrayUnLock(v);
end;

var
a, b: TMan;
v: Variant;
begin
a.name := 'Tom';
a.Age := 10;

RecordToVariant(a, SizeOf(a), v);

//把v发送到网络等等操作
//......

VariantToRecord(v, b, SizeOf(b));

writeln(b.name);
readln;
end.
 
接受答案了.
 
后退
顶部