管理员增加完成后,其他用户就可以看到多个教室了,如图:
我们看一下客户端增加教室的代码:
添加教室:
//声明一个教室实体类
ClassRoom classRoom = new ClassRoom(); classRoom.RoomName = txtClassRoom.Text.Trim(); classRoom.Department = this.currentUser.Department; //把教室类发送给客户端 (protobuf.net序列化) 并获取返回信息 ResMessage resMessage = tcpConnection.SendReceiveObject<ResMessage>("AddClassRoom", "ResAddClassRoom", 5000, classRoom); if (resMessage.Message == "操作成功") { txtClassRoom.Text = ""; GetRoomsByDep(); MessageBox.Show("添加教室成功"); } else { MessageBox.Show("出现错误,错误信息为:" + resMessage.Message); }
我们看一下服务器端的处理程序
构造函数中声明:
//学校管理员添加教室 NetworkComms.AppendGlobalIncomingPacketHandler<ClassRoom>("AddClassRoom", HandleAddClassRoom);
相关的处理方法:
//处理学校管理员添加教室 private void HandleAddClassRoom(PacketHeader header, Connection connection, ClassRoom classRoom) {
//保存到数据库中 DoClassRoom.Save(classRoom); ResMessage theMessage = new ResMessage(); theMessage.Message = "操作成功"; connection.SendObject("ResAddClassRoom", theMessage); }
DoCalssRoom方法
public class DoClassRoom { #region Private Methods /// <summary> /// Gets an instance of ClassRoom. /// </summary> /// <param name="id"> id </param> private static ClassRoom GetClassRoom( int id) { using (IDataReader reader = DBClassRoom.GetOne( id)) { return PopulateFromReader(reader); } } private static ClassRoom PopulateFromReader(IDataReader reader) { ClassRoom classRoom = new ClassRoom(); if (reader.Read()) { classRoom.Id = Convert.ToInt32(reader["Id"]); classRoom.RoomName = reader["RoomName"].ToString(); classRoom.DepID = Convert.ToInt32(reader["DepID"]); classRoom.Department = reader["Department"].ToString(); } return classRoom; } /// <summary> /// Persists a new instance of ClassRoom. Returns true on success. /// </summary> /// <returns></returns> private static bool Create(ClassRoom classRoom) { int newID = 0; newID = DBClassRoom.Create( classRoom.RoomName, classRoom.DepID, classRoom.Department); classRoom.Id = newID; return (newID > 0); } /// <summary> /// Updates this instance of ClassRoom. Returns true on success. /// </summary> /// <returns>bool</returns> private static bool Update(ClassRoom classRoom) { return DBClassRoom.Update( classRoom.Id, classRoom.RoomName, classRoom.DepID, classRoom.Department); } #endregion #region Public Methods /// <summary> /// Saves this instance of ClassRoom. Returns true on success. /// </summary> /// <returns>bool</returns> public static bool Save(ClassRoom classRoom) { if (classRoom.Id > 0) { return Update(classRoom); } else { return Create(classRoom); } } #endregion #region Static Methods /// <summary> /// Deletes an instance of ClassRoom. Returns true on success. /// </summary> /// <param name="id"> id </param> /// <returns>bool</returns> public static bool Delete( int id) { return DBClassRoom.Delete( id); } /// <summary> /// Gets a count of ClassRoom. /// </summary> public static int GetCount() { return DBClassRoom.GetCount(); } private static IList<ClassRoom> LoadListFromReader(IDataReader reader) { IList<ClassRoom> classRoomList = new List<ClassRoom>(); try { while (reader.Read()) { ClassRoom classRoom = new ClassRoom(); classRoom.Id = Convert.ToInt32(reader["Id"]); classRoom.RoomName = reader["RoomName"].ToString(); classRoom.DepID = Convert.ToInt32(reader["DepID"]); classRoom.Department = reader["Department"].ToString(); classRoomList.Add(classRoom); } } finally { reader.Close(); } return classRoomList; } /// <summary> /// Gets an IList with some instances of ClassRoom. /// </summary> public static IList<ClassRoom> GetTopList( int id) { IDataReader reader = DBClassRoom.GetTopList( id); return LoadListFromReader(reader); } /// <summary> /// Gets an IList with all instances of ClassRoom. /// </summary> public static IList<ClassRoom> GetAll() { IDataReader reader = DBClassRoom.GetAll(); return LoadListFromReader(reader); } /// <summary> /// Gets an IList with page of instances of ClassRoom. /// </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<ClassRoom> GetPage(int pageNumber, int pageSize, out int itemCount) { itemCount = 1; IDataReader reader = DBClassRoom.GetPage(pageNumber, pageSize, out itemCount); return LoadListFromReader(reader); } /// <summary> /// Gets an IList with page of instances of ClassRoom. /// </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<ClassRoom> GetListPage(int pageNumber, int pageSize, int pid, out int itemCount) { itemCount = 1; IDataReader reader = DBClassRoom.GetListPage(pageNumber, pageSize, pid, out itemCount); return LoadListFromReader(reader); } //根据用户单位获取单位的功能教室列表 public static IList<ClassRoom> GetClassRoomByUserDep(string depName) { IDataReader reader = DBClassRoom.GetClassRoomByDepName(depName); return LoadListFromReader(reader); } #endregion }
DBClassRoom类
public static class DBClassRoom { /// <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 ClassRoom table. Returns new integer id. /// </summary> /// <param name="roomName"> roomName </param> /// <param name="depID"> depID </param> /// <param name="department"> department </param> /// <returns>int</returns> public static int Create( string roomName, int depID, string department) { SqlParameterHelper sph = new SqlParameterHelper(GetWriteConnectionString(), "ClassRoom_Insert", 3); sph.DefineSqlParameter("@RoomName", SqlDbType.NVarChar, 200, ParameterDirection.Input, roomName); sph.DefineSqlParameter("@DepID", SqlDbType.Int, ParameterDirection.Input, depID); sph.DefineSqlParameter("@Department", SqlDbType.NVarChar, 200, ParameterDirection.Input, department); int newID = Convert.ToInt32(sph.ExecuteScalar()); return newID; } /// <summary> /// Updates a row in the ClassRoom table. Returns true if row updated. /// </summary> /// <param name="id"> id </param> /// <param name="roomName"> roomName </param> /// <param name="depID"> depID </param> /// <param name="department"> department </param> /// <returns>bool</returns> public static bool Update( int id, string roomName, int depID, string department) { SqlParameterHelper sph = new SqlParameterHelper(GetWriteConnectionString(), "ClassRoom_Update", 4); sph.DefineSqlParameter("@Id", SqlDbType.Int, ParameterDirection.Input, id); sph.DefineSqlParameter("@RoomName", SqlDbType.NVarChar, 200, ParameterDirection.Input, roomName); sph.DefineSqlParameter("@DepID", SqlDbType.Int, ParameterDirection.Input, depID); sph.DefineSqlParameter("@Department", SqlDbType.NVarChar, 200, ParameterDirection.Input, department); int rowsAffected = sph.ExecuteNonQuery(); return (rowsAffected > 0); } /// <summary> /// Deletes a row from the ClassRoom 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(), "ClassRoom_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 ClassRoom table. /// </summary> /// <param name="id"> id </param> public static IDataReader GetOne( int id) { SqlParameterHelper sph = new SqlParameterHelper(GetReadConnectionString(), "ClassRoom_SelectOne", 1); sph.DefineSqlParameter("@Id", SqlDbType.Int, ParameterDirection.Input, id); return sph.ExecuteReader(); } /// <summary> /// Gets an IDataReader with some list row from the ClassRoom table. /// </summary> /// <param name="id"> id </param> public static IDataReader GetTopList( int id) { SqlParameterHelper sph = new SqlParameterHelper(GetReadConnectionString(), "ClassRoom_SelectTopList", 1); sph.DefineSqlParameter("@pid", SqlDbType.Int, ParameterDirection.Input, id); return sph.ExecuteReader(); } /// <summary> /// Gets a count of rows in the ClassRoom table. /// </summary> public static int GetCount() { return Convert.ToInt32(SqlHelper.ExecuteScalar( GetReadConnectionString(), CommandType.StoredProcedure, "ClassRoom_GetCount", null)); } /// <summary> /// Gets a Listcount of rows in the ClassRoom table. /// </summary> public static int GetListCount(int pid) { SqlParameter theSqlParameter = new SqlParameter("@Pid", pid); return Convert.ToInt32(SqlHelper.ExecuteScalar( GetReadConnectionString(), CommandType.StoredProcedure, "ClassRoom_GetListCount", theSqlParameter)); } /// <summary> /// Gets an IDataReader with all rows in the ClassRoom table. /// </summary> public static IDataReader GetAll() { return SqlHelper.ExecuteReader( GetReadConnectionString(), CommandType.StoredProcedure, "ClassRoom_SelectAll", null); } //根据单位名称获取功能教师列表 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(); } /// <summary> /// Gets a page of data from the ClassRoom 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(), "ClassRoom_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(), "ClassRoom_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#源码)
【开源下载】基于winform的xml菜单编辑器(c#源码)