预约系统中,新用户可以自己注册,然后管理员开通。
注册界面如下:
客户端新用户注册代码:
private void button1_Click(object sender, EventArgs e) { if(txtUserID.Text.Trim ()==""|| txtUserName.Text.Trim ()=="") { MessageBox.Show ("用户名ID 和用户名不能为空"); return ; } //声明一个用户类 Users registerUser = new Users(); registerUser.UserID = txtUserID.Text.Trim(); registerUser.Name = txtUserName.Text.Trim(); registerUser.Department = cmbSchoolList.Text;
//声明一个初始密码,如果用户用此密码登陆,服务器可识别为未开通用户 registerUser.Password = "msdcmsdcmsdcmsdcmsdc";
//把实体类发送个客户端,并获取返回的结果 ResMessage resMessage = newTcpConnection.SendReceiveObject<ResMessage>("ReqRegUser", "ResRegUser", 5000, registerUser); if (resMessage.Message == "操作成功") { MessageBox.Show("新用户注册成功,请等待学校管理员审核,审核通过后初始密码为 123456"); this.Close(); } else { MessageBox.Show("出现错误,错误信息为:" + resMessage.Message); } }
服务器端的相关处理代码:
构造函数中声明:
//注册新用户 NetworkComms.AppendGlobalIncomingPacketHandler<Users>("ReqRegUser", HandlRegUser);
处理方法
private void HandlRegUser(PacketHeader header, Connection connection, Users Users) { string userID = Users.UserID; Users oldUser =DoUsers.GetUserByID(userID); // =-1代表不存在相关记录 if (oldUser.Id == -1) { DoUsers.Save(Users); ResMessage resMessage = new ResMessage("操作成功"); connection.SendObject("ResRegUser", resMessage); } else { ResMessage resMessage = new ResMessage("用户ID已经存在"); connection.SendObject("ResRegUser", resMessage); } }
DoUsers.GetUserByID方法
public static Users GetUserByID(string userID) { using (IDataReader reader = DBUsers.GetOneByUserID(userID)) { Users theUser = PopulateFromReader(reader); return theUser; } }
DoUsers中的Save方法
public static bool Save(Users Users) { if (Users.Id > 0) { return Update(Users); } else { return Create(Users); } }
Update和Create方法
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); }
DBUsers类
public static class DBUsers { /// <summary> /// Gets the connection string for read. /// </summary> /// <returns></returns> private static string GetReadConnectionString() { return ConfigurationManager.AppSettings["MSSQLConnectionString"]; } /// <summary> /// Gets the connection string for write. /// </summary> /// <returns></returns> private static string GetWriteConnectionString() { if (ConfigurationManager.AppSettings["MSSQLWriteConnectionString"] != null) { return ConfigurationManager.AppSettings["MSSQLWriteConnectionString"]; } return ConfigurationManager.AppSettings["MSSQLConnectionString"]; } /// <summary> /// Inserts a row in the RcUsers table. Returns new integer id. /// </summary> /// <param name="userID"> userID </param> /// <param name="name"> name </param> /// <param name="password"> password </param> /// <param name="declaring"> declaring </param> /// <param name="status"> status </param> /// <param name="isMale"> isMale </param> /// <param name="userLevel"> userLevel </param> /// <param name="enabled"> enabled </param> /// <param name="registerTime"> registerTime </param> /// <param name="lastLoginTime"> lastLoginTime </param> /// <param name="depID"> depID </param> /// <param name="department"> department </param> /// <returns>int</returns> public static int Create( string userID, string name, string password, string declaring, int status, bool isMale, int userLevel, bool enabled, DateTime registerTime, DateTime lastLoginTime, int depID, string department) { SqlParameterHelper sph = new SqlParameterHelper(GetWriteConnectionString(), "Users_Insert", 12); sph.DefineSqlParameter("@UserID", SqlDbType.NVarChar, 200, ParameterDirection.Input, userID); sph.DefineSqlParameter("@Name", SqlDbType.NVarChar, 200, ParameterDirection.Input, name); sph.DefineSqlParameter("@Password", SqlDbType.NVarChar, 200, ParameterDirection.Input, password); sph.DefineSqlParameter("@Declaring", SqlDbType.NVarChar, 200, ParameterDirection.Input, declaring); sph.DefineSqlParameter("@Status", SqlDbType.Int, ParameterDirection.Input, status); sph.DefineSqlParameter("@IsMale", SqlDbType.Bit, ParameterDirection.Input, isMale); sph.DefineSqlParameter("@UserLevel", SqlDbType.Int, ParameterDirection.Input, userLevel); sph.DefineSqlParameter("@Enabled", SqlDbType.Bit, ParameterDirection.Input, enabled); sph.DefineSqlParameter("@RegisterTime", SqlDbType.DateTime, ParameterDirection.Input, registerTime); sph.DefineSqlParameter("@LastLoginTime", SqlDbType.DateTime, ParameterDirection.Input, lastLoginTime); sph.DefineSqlParameter("@DepID", SqlDbType.Int, ParameterDirection.Input, depID); sph.DefineSqlParameter("@Department", SqlDbType.NVarChar, 100, ParameterDirection.Input, department); int newID = Convert.ToInt32(sph.ExecuteScalar()); return newID; } /// <summary> /// Updates a row in the RcUsers table. Returns true if row updated. /// </summary> /// <param name="id"> id </param> /// <param name="userID"> userID </param> /// <param name="name"> name </param> /// <param name="password"> password </param> /// <param name="declaring"> declaring </param> /// <param name="status"> status </param> /// <param name="isMale"> isMale </param> /// <param name="userLevel"> userLevel </param> /// <param name="enabled"> enabled </param> /// <param name="registerTime"> registerTime </param> /// <param name="lastLoginTime"> lastLoginTime </param> /// <param name="depID"> depID </param> /// <param name="department"> department </param> /// <returns>bool</returns> public static bool Update( int id, string userID, string name, string password, string declaring, int status, bool isMale, int userLevel, bool enabled, DateTime registerTime, DateTime lastLoginTime, int depID, string department) { SqlParameterHelper sph = new SqlParameterHelper(GetWriteConnectionString(), "Users_Update", 13); sph.DefineSqlParameter("@Id", SqlDbType.Int, ParameterDirection.Input, id); sph.DefineSqlParameter("@UserID", SqlDbType.NVarChar, 200, ParameterDirection.Input, userID); sph.DefineSqlParameter("@Name", SqlDbType.NVarChar, 200, ParameterDirection.Input, name); sph.DefineSqlParameter("@Password", SqlDbType.NVarChar, 200, ParameterDirection.Input, password); sph.DefineSqlParameter("@Declaring", SqlDbType.NVarChar, 200, ParameterDirection.Input, declaring); sph.DefineSqlParameter("@Status", SqlDbType.Int, ParameterDirection.Input, status); sph.DefineSqlParameter("@IsMale", SqlDbType.Bit, ParameterDirection.Input, isMale); sph.DefineSqlParameter("@UserLevel", SqlDbType.Int, ParameterDirection.Input, userLevel); sph.DefineSqlParameter("@Enabled", SqlDbType.Bit, ParameterDirection.Input, enabled); sph.DefineSqlParameter("@RegisterTime", SqlDbType.DateTime, ParameterDirection.Input, registerTime); sph.DefineSqlParameter("@LastLoginTime", SqlDbType.DateTime, ParameterDirection.Input, lastLoginTime); sph.DefineSqlParameter("@DepID", SqlDbType.Int, ParameterDirection.Input, depID); sph.DefineSqlParameter("@Department", SqlDbType.NVarChar, 100, ParameterDirection.Input, department); int rowsAffected = sph.ExecuteNonQuery(); return (rowsAffected > 0); } /// <summary> /// Deletes a row from the RcUsers table. Returns true if row deleted. /// </summary> /// <param name="id"> id </param> /// <returns>bool</returns> public static bool Delete( int id) { SqlParameterHelper sph = new SqlParameterHelper(GetWriteConnectionString(), "Users_Delete", 1); sph.DefineSqlParameter("@Id", SqlDbType.Int, ParameterDirection.Input, id); int rowsAffected = sph.ExecuteNonQuery(); return (rowsAffected > 0); } /// <summary> /// Gets an IDataReader with one row from the RcUsers table. /// </summary> /// <param name="id"> id </param> public static IDataReader GetOne( int id) { SqlParameterHelper sph = new SqlParameterHelper(GetReadConnectionString(), "Users_SelectOne", 1); sph.DefineSqlParameter("@Id", SqlDbType.Int, ParameterDirection.Input, id); return sph.ExecuteReader(); } //根据UserID获取记录 public static IDataReader GetOneByUserID(string userID) { SqlParameterHelper sph = new SqlParameterHelper(GetReadConnectionString(), "Users_SelectOneByUserID", 1); sph.DefineSqlParameter("@UserID", SqlDbType.NVarChar, 200, ParameterDirection.Input, userID); return sph.ExecuteReader(); } /// <summary> /// Gets an IDataReader with some list row from the RcUsers table. /// </summary> /// <param name="id"> id </param> public static IDataReader GetTopList( int id) { SqlParameterHelper sph = new SqlParameterHelper(GetReadConnectionString(), "Users_SelectTopList", 1); sph.DefineSqlParameter("@pid", SqlDbType.Int, ParameterDirection.Input, id); return sph.ExecuteReader(); } //根据用户单位获取本单位所有用户 public static IDataReader GetListByDep(string department) { SqlParameterHelper sph = new SqlParameterHelper(GetReadConnectionString(), "Users_SelectListByDep", 1); sph.DefineSqlParameter("@Department", SqlDbType.NVarChar, 100, ParameterDirection.Input, department); return sph.ExecuteReader(); } //根据用户单位获取本单位所有用户 新注册 public static IDataReader GetNewListByDep(string department) { SqlParameterHelper sph = new SqlParameterHelper(GetReadConnectionString(), "Users_SelectNewListByDep", 1); sph.DefineSqlParameter("@Department", SqlDbType.NVarChar, 100, ParameterDirection.Input, department); return sph.ExecuteReader(); } /// <summary> /// Gets a count of rows in the RcUsers table. /// </summary> public static int GetCount() { return Convert.ToInt32(SqlHelper.ExecuteScalar( GetReadConnectionString(), CommandType.StoredProcedure, "Users_GetCount", null)); } /// <summary> /// Gets a Listcount of rows in the RcUsers table. /// </summary> public static int GetListCount(int pid) { SqlParameter theSqlParameter = new SqlParameter("@Pid", pid); return Convert.ToInt32(SqlHelper.ExecuteScalar( GetReadConnectionString(), CommandType.StoredProcedure, "Users_GetListCount", theSqlParameter)); } /// <summary> /// Gets an IDataReader with all rows in the RcUsers table. /// </summary> public static IDataReader GetAll() { return SqlHelper.ExecuteReader( GetReadConnectionString(), CommandType.StoredProcedure, "Users_SelectAll", null); } /// <summary> /// Gets a page of data from the RcUsers table. /// </summary> /// <param name="pageNumber">The page number.</param> /// <param name="pageSize">Size of the page.</param> /// <param name="totalPages">total pages</param> public static IDataReader GetPage( int pageNumber, int pageSize, out int itemCount) { itemCount = GetCount(); SqlParameterHelper sph = new SqlParameterHelper(GetReadConnectionString(), "Users_SelectPage", 2); sph.DefineSqlParameter("@PageNumber", SqlDbType.Int, ParameterDirection.Input, pageNumber); sph.DefineSqlParameter("@PageSize", SqlDbType.Int, ParameterDirection.Input, pageSize); return sph.ExecuteReader(); } public static IDataReader GetListPage( int pageNumber, int pageSize, int pid, out int itemCount) { itemCount = GetListCount(pid); SqlParameterHelper sph = new SqlParameterHelper(GetReadConnectionString(), "Users_SelectListPage", 3); sph.DefineSqlParameter("@PageNumber", SqlDbType.Int, ParameterDirection.Input, pageNumber); sph.DefineSqlParameter("@PageSize", SqlDbType.Int, ParameterDirection.Input, pageSize); sph.DefineSqlParameter("@pid", SqlDbType.Int, ParameterDirection.Input, pid); return sph.ExecuteReader(); } }
【开源下载】基于TCP网络通信的即时聊天系统(IM系统)(c#源码)
【开源下载】基于TCP网络通信的自动升级程序c#源码
【开源下载】基于winform的xml菜单编辑器(c#源码)
【模板下载】分享我所使用的数据库框架