设置预约规则的客户端代码:
int stopHourInt = (int)stopHour.Value;
int stopMinuteInt = (int)stopMinute.Value;
//截止的时间
int stopTimeInt = stopHourInt * 100 + stopMinuteInt;
//提前的天数
int dayspan = (int)numDay.Value;
//声明一个ClassRoomConfig类 并设定具体的值
ClassRoomConfig classRoomConfig=new ClassRoomConfig ();
classRoomConfig.DaySpan=dayspan ;
classRoomConfig.StopTime=stopTimeInt;
classRoomConfig.Id = roomConfigID;
classRoomConfig.RoomID=this.itemID ;
//发送预约规则个服务器端,并获取返回结果
ResMessage resMessage=tcpConnnection.SendReceiveObject<ResMessage>("AddRoomConfig","ResRoomConfig",5000, classRoomConfig);
if (resMessage.Message == "操作成功")
{
GetCurrentRoomConfig();
MessageBox.Show("添加设置完成");
}
else
{
MessageBox.Show("出现错误,错误为" + resMessage.Message);
}
服务器端处理代码如下:
构造函数中声明:
//学校管理员添加教室的相关设置
NetworkComms.AppendGlobalIncomingPacketHandler<ClassRoomConfig>("AddRoomConfig", HandleAddRoomConfig);
处理方法:
private void HandleAddRoomConfig(PacketHeader header, Connection connection, ClassRoomConfig classRoomConfig)
{
DoClassRoomConfig.Save(classRoomConfig);
ResMessage theMessage = new ResMessage();
theMessage.Message = "操作成功";
connection.SendObject("ResRoomConfig", theMessage);
}
操作类:
public class DoClassRoomConfig
{
#region Private Methods
/// <summary>
/// Gets an instance of ClassRoomConfig.
/// </summary>
/// <param name="id"> id </param>
private static ClassRoomConfig GetClassRoomConfig(
int id)
{
using (IDataReader reader = DBClassRoomConfig.GetOne(
id))
{
return PopulateFromReader(reader);
}
}
public static ClassRoomConfig GetClassRoomConfigByRoomID(
int roomID)
{
using (IDataReader reader = DBClassRoomConfig.GetOneByRoomID(
roomID))
{
return PopulateFromReaderF(reader);
}
}
private static ClassRoomConfig PopulateFromReader(IDataReader reader)
{
ClassRoomConfig classRoomConfig = new ClassRoomConfig();
if (reader.Read())
{
classRoomConfig.Id = Convert.ToInt32(reader["Id"]);
classRoomConfig.DaySpan = Convert.ToInt32(reader["DaySpan"]);
classRoomConfig.StopTime = Convert.ToInt32(reader["StopTime"]);
classRoomConfig.RoomID = Convert.ToInt32(reader["RoomID"]);
classRoomConfig.RoomName = reader["RoomName"].ToString();
classRoomConfig.ModifyDate = Convert.ToDateTime(reader["ModifyDate"]);
}
return classRoomConfig;
}
private static ClassRoomConfig PopulateFromReaderF(IDataReader reader)
{
ClassRoomConfig classRoomConfig = new ClassRoomConfig();
if (reader.Read())
{
classRoomConfig.Id = Convert.ToInt32(reader["Id"]);
classRoomConfig.DaySpan = Convert.ToInt32(reader["DaySpan"]);
classRoomConfig.StopTime = Convert.ToInt32(reader["StopTime"]);
string stopTimeStr = classRoomConfig.StopTime.ToString();
classRoomConfig.RoomName = stopTimeStr.Insert(stopTimeStr.Length - 2, "点") + "分";
classRoomConfig.RoomID = Convert.ToInt32(reader["RoomID"]);
classRoomConfig.ModifyDate = Convert.ToDateTime(reader["ModifyDate"]);
}
return classRoomConfig;
}
/// <summary>
/// Persists a new instance of ClassRoomConfig. Returns true on success.
/// </summary>
/// <returns></returns>
private static bool Create(ClassRoomConfig classRoomConfig)
{
int newID = 0;
newID = DBClassRoomConfig.Create(
classRoomConfig.DaySpan,
classRoomConfig.StopTime,
classRoomConfig.RoomID,
classRoomConfig.RoomName,
classRoomConfig.ModifyDate);
classRoomConfig.Id = newID;
return (newID > 0);
}
/// <summary>
/// Updates this instance of ClassRoomConfig. Returns true on success.
/// </summary>
/// <returns>bool</returns>
private static bool Update(ClassRoomConfig classRoomConfig)
{
return DBClassRoomConfig.Update(
classRoomConfig.Id,
classRoomConfig.DaySpan,
classRoomConfig.StopTime,
classRoomConfig.RoomID,
classRoomConfig.RoomName,
classRoomConfig.ModifyDate);
}
#endregion
#region Public Methods
/// <summary>
/// Saves this instance of ClassRoomConfig. Returns true on success.
/// </summary>
/// <returns>bool</returns>
public static bool Save(ClassRoomConfig classRoomConfig)
{
if (classRoomConfig.Id > 0)
{
return Update(classRoomConfig);
}
else
{
return Create(classRoomConfig);
}
}
#endregion
#region Static Methods
/// <summary>
/// Deletes an instance of ClassRoomConfig. Returns true on success.
/// </summary>
/// <param name="id"> id </param>
/// <returns>bool</returns>
public static bool Delete(
int id)
{
return DBClassRoomConfig.Delete(
id);
}
/// <summary>
/// Gets a count of ClassRoomConfig.
/// </summary>
public static int GetCount()
{
return DBClassRoomConfig.GetCount();
}
private static IList<ClassRoomConfig> LoadListFromReader(IDataReader reader)
{
IList<ClassRoomConfig> classRoomConfigList = new List<ClassRoomConfig>();
try
{
while (reader.Read())
{
ClassRoomConfig classRoomConfig = new ClassRoomConfig();
classRoomConfig.Id = Convert.ToInt32(reader["Id"]);
classRoomConfig.DaySpan = Convert.ToInt32(reader["DaySpan"]);
classRoomConfig.StopTime = Convert.ToInt32(reader["StopTime"]);
classRoomConfig.RoomID = Convert.ToInt32(reader["RoomID"]);
classRoomConfig.RoomName = reader["RoomName"].ToString();
classRoomConfig.ModifyDate = Convert.ToDateTime(reader["ModifyDate"]);
classRoomConfigList.Add(classRoomConfig);
}
}
finally
{
reader.Close();
}
return classRoomConfigList;
}
/// <summary>
/// Gets an IList with some instances of ClassRoomConfig.
/// </summary>
public static IList<ClassRoomConfig> GetTopList(
int id)
{
IDataReader reader = DBClassRoomConfig.GetTopList(
id);
return LoadListFromReader(reader);
}
/// <summary>
/// Gets an IList with all instances of ClassRoomConfig.
/// </summary>
public static IList<ClassRoomConfig> GetAll()
{
IDataReader reader = DBClassRoomConfig.GetAll();
return LoadListFromReader(reader);
}
/// <summary>
/// Gets an IList with page of instances of ClassRoomConfig.
/// </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<ClassRoomConfig> GetPage(int pageNumber, int pageSize, out int itemCount)
{
itemCount = 1;
IDataReader reader = DBClassRoomConfig.GetPage(pageNumber, pageSize, out itemCount);
return LoadListFromReader(reader);
}
/// <summary>
/// Gets an IList with page of instances of ClassRoomConfig.
/// </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<ClassRoomConfig> GetListPage(int pageNumber, int pageSize, int pid, out int itemCount)
{
itemCount = 1;
IDataReader reader = DBClassRoomConfig.GetListPage(pageNumber, pageSize, pid, out itemCount);
return LoadListFromReader(reader);
}
#endregion
}
数据库类:
public static class DBClassRoomConfig
{
/// <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 ClassRoomConfig table. Returns new integer id.
/// </summary>
/// <param name="daySpan"> daySpan </param>
/// <param name="stopTime"> stopTime </param>
/// <param name="roomID"> roomID </param>
/// <param name="roomName"> roomName </param>
/// <param name="modifyDate"> modifyDate </param>
/// <returns>int</returns>
public static int Create(
int daySpan,
int stopTime,
int roomID,
string roomName,
DateTime modifyDate)
{
SqlParameterHelper sph = new SqlParameterHelper(GetWriteConnectionString(), "ClassRoomConfig_Insert", 5);
sph.DefineSqlParameter("@DaySpan", SqlDbType.Int, ParameterDirection.Input, daySpan);
sph.DefineSqlParameter("@StopTime", SqlDbType.Int, ParameterDirection.Input, stopTime);
sph.DefineSqlParameter("@RoomID", SqlDbType.Int, ParameterDirection.Input, roomID);
sph.DefineSqlParameter("@RoomName", SqlDbType.NVarChar, 200, ParameterDirection.Input, roomName);
sph.DefineSqlParameter("@ModifyDate", SqlDbType.DateTime, ParameterDirection.Input, modifyDate);
int newID = Convert.ToInt32(sph.ExecuteScalar());
return newID;
}
/// <summary>
/// Updates a row in the ClassRoomConfig table. Returns true if row updated.
/// </summary>
/// <param name="id"> id </param>
/// <param name="daySpan"> daySpan </param>
/// <param name="stopTime"> stopTime </param>
/// <param name="roomID"> roomID </param>
/// <param name="roomName"> roomName </param>
/// <param name="modifyDate"> modifyDate </param>
/// <returns>bool</returns>
public static bool Update(
int id,
int daySpan,
int stopTime,
int roomID,
string roomName,
DateTime modifyDate)
{
SqlParameterHelper sph = new SqlParameterHelper(GetWriteConnectionString(), "ClassRoomConfig_Update", 6);
sph.DefineSqlParameter("@Id", SqlDbType.Int, ParameterDirection.Input, id);
sph.DefineSqlParameter("@DaySpan", SqlDbType.Int, ParameterDirection.Input, daySpan);
sph.DefineSqlParameter("@StopTime", SqlDbType.Int, ParameterDirection.Input, stopTime);
sph.DefineSqlParameter("@RoomID", SqlDbType.Int, ParameterDirection.Input, roomID);
sph.DefineSqlParameter("@RoomName", SqlDbType.NVarChar, 200, ParameterDirection.Input, roomName);
sph.DefineSqlParameter("@ModifyDate", SqlDbType.DateTime, ParameterDirection.Input, modifyDate);
int rowsAffected = sph.ExecuteNonQuery();
return (rowsAffected > 0);
}
/// <summary>
/// Deletes a row from the ClassRoomConfig 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(), "ClassRoomConfig_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 ClassRoomConfig table.
/// </summary>
/// <param name="id"> id </param>
public static IDataReader GetOne(
int id)
{
SqlParameterHelper sph = new SqlParameterHelper(GetReadConnectionString(), "ClassRoomConfig_SelectOne", 1);
sph.DefineSqlParameter("@Id", SqlDbType.Int, ParameterDirection.Input, id);
return sph.ExecuteReader();
}
//ClassRoomConfig_SelectOneByRoomID
public static IDataReader GetOneByRoomID(
int roomID)
{
SqlParameterHelper sph = new SqlParameterHelper(GetReadConnectionString(), "ClassRoomConfig_SelectOneByRoomID", 1);
sph.DefineSqlParameter("@RoomID", SqlDbType.Int, ParameterDirection.Input, roomID);
return sph.ExecuteReader();
}
/// <summary>
/// Gets an IDataReader with some list row from the ClassRoomConfig table.
/// </summary>
/// <param name="id"> id </param>
public static IDataReader GetTopList(
int id)
{
SqlParameterHelper sph = new SqlParameterHelper(GetReadConnectionString(), "ClassRoomConfig_SelectTopList", 1);
sph.DefineSqlParameter("@pid", SqlDbType.Int, ParameterDirection.Input, id);
return sph.ExecuteReader();
}
/// <summary>
/// Gets a count of rows in the ClassRoomConfig table.
/// </summary>
public static int GetCount()
{
return Convert.ToInt32(SqlHelper.ExecuteScalar(
GetReadConnectionString(),
CommandType.StoredProcedure,
"ClassRoomConfig_GetCount",
null));
}
/// <summary>
/// Gets a Listcount of rows in the ClassRoomConfig table.
/// </summary>
public static int GetListCount(int pid)
{
SqlParameter theSqlParameter = new SqlParameter("@Pid", pid);
return Convert.ToInt32(SqlHelper.ExecuteScalar(
GetReadConnectionString(),
CommandType.StoredProcedure,
"ClassRoomConfig_GetListCount",
theSqlParameter));
}
/// <summary>
/// Gets an IDataReader with all rows in the ClassRoomConfig table.
/// </summary>
public static IDataReader GetAll()
{
return SqlHelper.ExecuteReader(
GetReadConnectionString(),
CommandType.StoredProcedure,
"ClassRoomConfig_SelectAll",
null);
}
/// <summary>
/// Gets a page of data from the ClassRoomConfig 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(), "ClassRoomConfig_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(), "ClassRoomConfig_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();
}
}