客户端代码:
//发出的数据类型为int类型 ItemID为用户对应的ID ResMessage resMessage = tcpConnnection.SendReceiveObject<ResMessage>("ReqResetPsw", "ResResetPsw", 5000, itemID); if (resMessage.Message == "操作成功") { paramHelper.ChangeParam("操作成功"); MessageBox.Show("操作成功,用户密码为:123456"); button1.Enabled = false; } else { MessageBox.Show("出现错误,错误信息为:" + resMessage.Message); }
服务器端代码:
//客户端管理员修改用户的密码 NetworkComms.AppendGlobalIncomingPacketHandler<int>("ReqResetPsw", HandleResetPassword);
处理方法:
//根据管理员提供的用户ID修改相应用户的密码 private void HandleResetPassword(PacketHeader header, Connection connection, int itemID) { Users theUser= DoUsers.GetUsers(itemID); //把密码恢复成123456 theUser.Password = "123456"; DoUsers.Save(theUser); ResMessage theMessage = new ResMessage(); theMessage.Message = "操作成功"; connection.SendObject("ResResetPsw", theMessage); }
操作类:
public class DoUsers { #region Private Methods /// <summary> /// Gets an instance of Users. /// </summary> /// <param name="id"> id </param> public static Users GetUsers( int id) { using (IDataReader reader = DBUsers.GetOne( id)) { return PopulateFromReader(reader); } } private static Users PopulateFromReader(IDataReader reader) { Users Users = new Users(); if (reader.Read()) { Users.Id = Convert.ToInt32(reader["Id"]); Users.UserID = reader["UserID"].ToString(); Users.Name = reader["Name"].ToString(); Users.Password = reader["Password"].ToString(); Users.Declaring = reader["Declaring"].ToString(); Users.Status = Convert.ToInt32(reader["Status"]); Users.IsMale = Convert.ToBoolean(reader["IsMale"]); Users.UserLevel = Convert.ToInt32(reader["UserLevel"]); Users.Enabled = Convert.ToBoolean(reader["Enabled"]); Users.RegisterTime = Convert.ToDateTime(reader["RegisterTime"]); Users.LastLoginTime = Convert.ToDateTime(reader["LastLoginTime"]); Users.DepID = Convert.ToInt32(reader["DepID"]); Users.Department = reader["Department"].ToString(); } return Users; } /// <summary> /// Persists a new instance of Users. Returns true on success. /// </summary> /// <returns></returns> private static bool Create(Users Users) { int newID = 0; newID = DBUsers.Create( Users.UserID, Users.Name, Users.Password, Users.Declaring, Users.Status, Users.IsMale, Users.UserLevel, Users.Enabled, Users.RegisterTime, Users.LastLoginTime, Users.DepID, Users.Department); Users.Id = newID; return (newID > 0); } /// <summary> /// Updates this instance of Users. Returns true on success. /// </summary> /// <returns>bool</returns> private static bool Update(Users Users) { return DBUsers.Update( Users.Id, Users.UserID, Users.Name, Users.Password, Users.Declaring, Users.Status, Users.IsMale, Users.UserLevel, Users.Enabled, Users.RegisterTime, Users.LastLoginTime, Users.DepID, Users.Department); } #endregion #region Public Methods /// <summary> /// Saves this instance of Users. Returns true on success. /// </summary> /// <returns>bool</returns> public static bool Save(Users Users) { if (Users.Id > 0) { return Update(Users); } else { return Create(Users); } } #endregion #region Static Methods /// <summary> /// Deletes an instance of Users. Returns true on success. /// </summary> /// <param name="id"> id </param> /// <returns>bool</returns> public static bool Delete( int id) { return DBUsers.Delete( id); } /// <summary> /// Gets a count of Users. /// </summary> public static int GetCount() { return DBUsers.GetCount(); } private static IList<Users> LoadListFromReader(IDataReader reader) { IList<Users> UsersList = new List<Users>(); try { while (reader.Read()) { Users Users = new Users(); Users.Id = Convert.ToInt32(reader["Id"]); Users.UserID = reader["UserID"].ToString(); Users.Name = reader["Name"].ToString(); Users.Password = reader["Password"].ToString(); Users.Declaring = reader["Declaring"].ToString(); Users.Status = Convert.ToInt32(reader["Status"]); Users.IsMale = Convert.ToBoolean(reader["IsMale"]); Users.UserLevel = Convert.ToInt32(reader["UserLevel"]); Users.Enabled = Convert.ToBoolean(reader["Enabled"]); Users.RegisterTime = Convert.ToDateTime(reader["RegisterTime"]); Users.LastLoginTime = Convert.ToDateTime(reader["LastLoginTime"]); Users.DepID = Convert.ToInt32(reader["DepID"]); Users.Department = reader["Department"].ToString(); UsersList.Add(Users); } } finally { reader.Close(); } return UsersList; } //客户端获取用户信息,只返回ID,UserID, UserName,RegisterTime private static IList<Users> LoadPartDataListFromReader(IDataReader reader) { IList<Users> UsersList = new List<Users>(); try { while (reader.Read()) { Users Users = new Users(); Users.Id = Convert.ToInt32(reader["Id"]); Users.UserID = reader["UserID"].ToString(); Users.Name = reader["Name"].ToString(); Users.RegisterTime = Convert.ToDateTime(reader["RegisterTime"]); UsersList.Add(Users); } } finally { reader.Close(); } return UsersList; } /// <summary> /// Gets an IList with some instances of Users. /// </summary> public static IList<Users> GetTopList( int id) { IDataReader reader = DBUsers.GetTopList( id); return LoadListFromReader(reader); } /// <summary> /// Gets an IList with all instances of Users. /// </summary> public static IList<Users> GetAll() { IDataReader reader = DBUsers.GetAll(); return LoadListFromReader(reader); } //根据用户单位获取本单位所有用户的数据 public static IList<Users> GetUserByDep(string department) { IDataReader reader = DBUsers.GetListByDep(department); return LoadPartDataListFromReader(reader); } //根据用户单位获取本单位所有用户的数据 新注册的 public static IList<Users> GetNewUserByDep(string department) { IDataReader reader = DBUsers.GetNewListByDep(department); return LoadPartDataListFromReader(reader); } /// <summary> /// Gets an IList with page of instances of Users. /// </summary> /// <param name="pageNumber">The page number.</param> /// <param name="pageSize">Size of the page.</param> /// <param name="totalPages">total pages</param> public static IList<Users> GetPage(int pageNumber, int pageSize, out int itemCount) { itemCount = 1; IDataReader reader = DBUsers.GetPage(pageNumber, pageSize, out itemCount); return LoadListFromReader(reader); } /// <summary> /// Gets an IList with page of instances of Users. /// </summary> /// <param name="pageNumber">The page number.</param> /// <param name="pageSize">Size of the page.</param> /// <param name="itemCount">total items</param> public static IList<Users> GetListPage(int pageNumber, int pageSize, int pid, out int itemCount) { itemCount = 1; IDataReader reader = DBUsers.GetListPage(pageNumber, pageSize, pid, out itemCount); return LoadListFromReader(reader); } #endregion #region 新加方法 public static Users GetUserByID(string userID) { using (IDataReader reader = DBUsers.GetOneByUserID(userID)) { Users theUser = PopulateFromReader(reader); return theUser; } } #endregion }
Users类:
[ProtoContract] public class Users { #region Constructors public Users() { } #endregion #region Private Properties private int id = -1; private string userID = string.Empty; private string name = string.Empty; private string password = string.Empty; private string declaring = string.Empty; private int status = -1; private bool isMale = false; private int userLevel = -1; private bool enabled = false; private DateTime registerTime = DateTime.UtcNow; private DateTime lastLoginTime = DateTime.UtcNow; private int depID = -1; private string department = string.Empty; #endregion #region Public Properties [ProtoMember(1)] public int Id { get { return id; } set { id = value; } } [ProtoMember(2)] public string UserID { get { return userID; } set { userID = value; } } [ProtoMember(3)] public string Name { get { return name; } set { name = value; } } [ProtoMember(4)] public string Password { get { return password; } set { password = value; } } [ProtoMember(5)] public string Declaring { get { return declaring; } set { declaring = value; } } [ProtoMember(6)] public int Status { get { return status; } set { status = value; } } [ProtoMember(7)] public bool IsMale { get { return isMale; } set { isMale = value; } } [ProtoMember(8)] public int UserLevel { get { return userLevel; } set { userLevel = value; } } [ProtoMember(9)] public bool Enabled { get { return enabled; } set { enabled = value; } } [ProtoMember(10)] public DateTime RegisterTime { get { return registerTime; } set { registerTime = value; } } [ProtoMember(11)] public DateTime LastLoginTime { get { return lastLoginTime; } set { lastLoginTime = value; } } [ProtoMember(12)] public int DepID { get { return depID; } set { depID = value; } } [ProtoMember(13)] public string Department { get { return department; } set { department = value; } } #endregion }
【开源下载】基于TCP网络通信的即时聊天系统(IM系统)(c#源码)
【开源下载】基于winform的xml菜单编辑器(c#源码)