界面如下:
客户端代码:
//用这2个属性映射新密码和旧密码
//原密码
this.currentUser.Password = textBox1.Text.Trim();
//新密码
this.currentUser.NewPassword = textBox2.Text.Trim();
//把带有密码信息的契约类 currentUser发送到服务器端,并获取返回结果
ResMessage resMessage = newTcpConnection.SendReceiveObject<ResMessage>("ChangePassword", "ResChangePassword", 5000, this.currentUser);
if (resMessage.Message == "操作成功")
{
MessageBox.Show("密码更新成功");
}
else
{
MessageBox.Show("出现错误,错误原因为:" + resMessage.Message);
}
服务器端代码:
在构造函数中声明更改密码的处理方法:
//用户更改密码
NetworkComms.AppendGlobalIncomingPacketHandler<Users>("ChangePassword", HandleChangePassword);
服务器端的处理方法:
private void HandleChangePassword(PacketHeader header, Connection connection, Users theUser)
{
//首先从数据库中获取相应的用户
Users currentUser = DoUsers.GetUserByID(theUser.UserID);
ResMessage resMessage = new ResMessage();
resMessage.Message = "出现未知错误";
//判断原密码是否正确 if (currentUser.Password == theUser.Password)
{
//如果原密码正确,则设置新密码
currentUser.Password = theUser.NewPassword;
DoUsers.Save(currentUser);
resMessage.Message = "操作成功";
}
else
{
resMessage.Message = "原密码不正确";
}
//把获取到的数据发回去
connection.SendObject("ResChangePassword", resMessage);
}
数据库操作类:
![](http://static.oschina.net/uploads/img/201503/03144415_70Hh.gif)
![](http://static.oschina.net/uploads/img/201503/03144415_pthG.gif)
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
}
客户端收到服务器端返回的消息,弹出提示窗口:
【开源下载】基于TCP网络通信的即时聊天系统(IM系统)(c#源码)
【开源下载】基于winform的xml菜单编辑器(c#源码)