客户端用户登陆后,在主界面中可以看到本单位的所有教室,我们来看一下是如何获取的:
客户端代码:
//根据用户获取功能教室列表 private void GetRooms(Users user) { //把当前用户信息发送个服务器端,并获取到本用户相关的教室列表 RoomList roomList = newTcpConnection.SendReceiveObject<RoomList>("GetRooms", "RoomList", 5000, user); IList<ClassRoom> classRomms= roomList.ClassRooms; listBox1.DataSource = classRomms; listBox1.DisplayMember = "RoomName"; listBox1.ValueMember = "ID"; }
RoomList类 (可由protobuf.net 序列化)
//根据单位名称获取功能室列表 [ProtoContract] public class RoomList { [ProtoMember(1)] public IList<ClassRoom> ClassRooms; public RoomList() { } public RoomList(IList<ClassRoom> rooms) { this.ClassRooms = rooms; } }
服务器端的处理方法:
在构造函数中声明:
//根据用户的单位,获取所有功能教室 列表 NetworkComms.AppendGlobalIncomingPacketHandler<Users>("GetRooms", HandleGetRooms);
处理方法:
private void HandleGetRooms(PacketHeader header, Connection connection, Users theUser) { IList<ClassRoom> theRooms = DoClassRoom.GetClassRoomByUserDep(theUser.Department); RoomList roomList = new RoomList(theRooms); //把获取到的数据发回去 connection.SendObject("RoomList", roomList); }
DoClassRoom.GetClassRoomByUserDep方法
//根据用户单位获取单位的功能教室列表 public static IList<ClassRoom> GetClassRoomByUserDep(string depName) { IDataReader reader = DBClassRoom.GetClassRoomByDepName(depName); return LoadListFromReader(reader); }
LoadListFromReader方法
DBClassRoom.GetClassRoomByDepName方法
//根据单位名称获取功能教师列表 public static IDataReader GetClassRoomByDepName(string department) { SqlParameterHelper sph = new SqlParameterHelper(GetWriteConnectionString(), "ClassRoom_SelectClassRoomByDepName", 1); sph.DefineSqlParameter("@Department", SqlDbType.NVarChar, 200, ParameterDirection.Input, department); return sph.ExecuteReader(); }
相关存储过程:
CREATE PROCEDURE [dbo].ClassRoom_SelectClassRoomByDepName @Department nvarchar(200) AS SELECT [Id], [RoomName], [DepID], [Department] FROM [dbo].[ClassRoom] where Department=@Department