关于DATASNAP传递Record类型到客户端的问题

来源:互联网 发布:七天爱上你在哪网络看 编辑:程序博客网 时间:2024/05/22 06:27

以前,使用 DELPHI 2010 时,可以使用类似如下的代码来传递);


I wish to be able to declare a Data Snap method with the following signature


type  TLoginInfo =record    Username:string;    Password:string;    LastLogged: DateTime;end;function GetLoginInfo(const UserId: Integer): TLoginInfo;




When I try to call it it says that TLoginInfo is not well known.

Answer:


store the record into a stream and pass the stream to the DataSnap method



//on server sidefunction GetLoginInfo(const UserId: Integer): TStream;begin  Result := TMemoryStream.Create;  Result.Write( loginRec, SizeOf(TLoginInfo))  Result.Seek(0, TSeekOrigin.soBeginning);end;


//on client sideprocedure TfrmMain.getLogInto( sUser:string);var  AStr : TStream;  loginRec : TLoginInfo;begin//  cycleConnection;with TMethodsClient.Create( SQLConn.DBXConnection,False)dobegin    AStr := GetLoginInfo( sUser );    AStr.Read( loginRec, SizeOf(TLoginInfo))    Free;end;  FreeAndNil(AStr);end;

---------------------------

即使用 流的方法的进行传递。

可是以上的代码,在DELPHI XE 时好象就能不通过了(如果我没有记错的话),当然,稍微调整一下,也还是能通过的;

即将:

type  TLoginInfo =record    Username:string;    Password:string;    LastLogged: DateTime;end;

改成类似:

type  TLoginInfo =record    Username:string[20];    Password:string[20];    LastLogged: DateTime;end;

直到现在 XE6 仍然可以用。

但是使用 XE6 的一个重要的原因,就是使用它来开发 Android 的应用。

在安卓的应用中类似 String[20]的类型是不被支持的。。

看来,只能使用其它的方法。。。于是想到了使用 OleVariant


具体代码如下:

服务器端:


    TLoginUserInfo = record      LoginState        : Integer ;   //登录状态:0成功,1密码错误,2用户不存在,3用户被禁用,255未知错误      IsSuperUser       : Boolean;      FactoryEvaluation : Boolean;    //验厂状态,当为TRUE时,仅显示报程表,否则正常;      UserCode          : string;      UserName          : string;      Password          : string;      Deportment        : string;      Barcode           : string;      MachineCode       : string;      AutoPromptMsg     : string;      UserClass         : integer ;  //用户类型,0普通用户 1印前 2印后 3手工      ProductionWorkShop: string;  end;.......function ChekcUsernameAndPassword2(U,P:string):OleVariant;var  UserInfo :TLoginUserInfo;  V:Variant ;begin  UserInfo.LoginState := 255 ;   //Defautl: Unknown error  with QRY  do begin    Close;    SQL.Text :='';//省略    Open;    if not IsEmpty then begin      if P <> FieldByName('MM').AsString  then begin        UserInfo.LoginState :=1;      end else      if FieldByName('SFJY').AsBoolean then begin        UserInfo.LoginState :=3;      end else begin        UserInfo.LoginState :=0;        UserInfo.IsSuperUser := FieldByName('CJYH').AsBoolean;        UserInfo.UserCode := U;        UserInfo.UserCode := FieldByName('XH').AsString;        UserInfo.UserName := FieldByName('MC').AsString;        UserInfo.Password := FieldByName('MM').AsString;        UserInfo.Deportment := FieldByName('BMBH').AsString;        UserInfo.MachineCode := FieldByName('SBBH').AsString;  //设备编号        UserInfo.AutoPromptMsg :='';        UserInfo.UserClass :=FieldByName('jtsclx').AsInteger ;        UserInfo.ProductionWorkShop :=FieldByName('SCCJ').AsString ;      end;    end else UserInfo.LoginState :=2;    Close;    //读是否是验厂    SQL.Text :='';//省略    Open;    UserInfo.FactoryEvaluation := FieldByName('FE').AsBoolean;    Close;  end;  V := VarArrayCreate([0,8],VarVariant);  v[0] := UserInfo.LoginState ;  v[1] := UserInfo.IsSuperUser ;  v[2] :=UserInfo.UserCode ;  v[3] :=UserInfo.UserName  ;  v[4] :=UserInfo.Password ;  v[5] :=UserInfo.Deportment ;  v[6] :=UserInfo.MachineCode;  v[7] :=UserInfo.AutoPromptMsg ;  v[8] :=UserInfo.ProductionWorkShop ;  Result := V;end;

客户端:


vardm :TServerMethods1Client;procedure Tdm.DataModuleCreate(Sender: TObject);begindm := TServerMethods1Client.Create(SQLCnt_1.DBXConnection);
end;function Tdm.PubF_CheckLoginUsername2(U, P: string): TLoginUserInfo;var  V:Variant ;begin  V := dm.ChekcUsernameAndPassword2(U,P);  if VarIsArray(V) then begin    Result.LoginState         := v[0];    Result.IsSuperUser        := v[1];    Result.UserCode           := v[2];    Result.UserName           := v[3];    Result.Password           := v[4];    Result.Deportment         := v[5];    Result.MachineCode        := v[6];    Result.AutoPromptMsg      := v[7];    Result.ProductionWorkShop := v[8];  end;end;

以上代码,服务器端 windows7 / windows2008 r2   客户端:windows7  、 小米3(MIUI5)中测试通过;

0 0
原创粉丝点击