62,242
社区成员




public class TheGlobalMsgs
{
const long timerDelay = 60000;//定时器过期时间60秒(1分钟内的短消息保留),客户端最长每30秒一取!
Timer theTimer;
public static DataTable theMsgsTable = null;
public TheGlobalMsgs()
{
theMsgsTable = new DataTable("TheMsgTable"); //| 建立表, 表名:"Msgs"
System.Data.DataColumn Column1 = new DataColumn("id", typeof(System.Int64)); //| 建立字段, 字段名:"id", 类型: int
System.Data.DataColumn Column2 = new DataColumn("msgText", typeof(System.String));
System.Data.DataColumn Column3 = new DataColumn("GetUserID", typeof(System.String));
System.Data.DataColumn Column4 = new DataColumn("SendUserID", typeof(System.String));
System.Data.DataColumn Column5 = new DataColumn("SendUserName", typeof(System.String));
System.Data.DataColumn Column6 = new DataColumn("SendTime", typeof(System.DateTime));
Column1.AutoIncrement = true; //| 设置自增ID
Column1.AutoIncrementSeed = 1; //| ID 起始值
Column1.AutoIncrementStep = 1; //| 递增量
theMsgsTable.Columns.Add(Column1);
theMsgsTable.Columns.Add(Column2);
theMsgsTable.Columns.Add(Column3);
theMsgsTable.Columns.Add(Column4);
theMsgsTable.Columns.Add(Column5);
theMsgsTable.Columns.Add(Column6);
//我想在内存中动态创建一个表
// 找到这么一段代码 请高手如何创建一个完整的表 包括创建字段,设置主键,设置字段类型 等等
theMsgsTable.PrimaryKey = new System.Data.DataColumn[] { Column1 };
TimerCallback OnTimerTick = new TimerCallback(TimerTick);
theTimer = new Timer(OnTimerTick, null, timerDelay, timerDelay);
}
/// <summary>
/// 定时期过期方法 删除表中的两分钟以前的过期值
/// </summary>
/// <param name="state"></param>
private void TimerTick(object state)
{
DataView theDV = new DataView(theMsgsTable);
theDV.AllowDelete = true;
theDV.AllowEdit = true;
theDV.AllowNew = true;
theDV.RowFilter = "SendTime < #" + DateTime.Now.AddMilliseconds(-120000).ToLongTimeString() + "#" ;
for (int i = 0; i < theDV.Count; i++)
{
theDV.Delete(i);
}
theMsgsTable.AcceptChanges();
}
/// <summary>
/// 向内存表中添加一个消息
/// </summary>
/// <param name="inp_SendUser_ID"></param>
/// <param name="inp_SendUser_Name"></param>
/// <param name="inp_GetUser_ID"></param>
/// <param name="inp_TheMsgText"></param>
/// <returns></returns>
public bool AddMsg(string inp_SendUser_ID, string inp_SendUser_Name, string inp_GetUser_ID, string inp_TheMsgText)
{
if (inp_GetUser_ID == null || inp_GetUser_ID == "" || !DAL.DBBase.IsInt(inp_GetUser_ID) || inp_TheMsgText == null || inp_TheMsgText == "")
{
return false;
}
if (inp_SendUser_ID != null && inp_SendUser_ID != "")
{
if (!DAL.DBBase.IsInt(inp_SendUser_ID))
{
return false;
}
}
System.Data.DataRow theRow = theMsgsTable.NewRow(); //新行;
//theRow[id] = ""; 行ID可以自加得到!!!!
theRow["msgText"] = inp_TheMsgText;
theRow["GetUserID"] = inp_GetUser_ID;
theRow["SendUserID"] = inp_SendUser_ID;
theRow["SendUserName"] = inp_SendUser_Name;
theRow["SendTime"] = System.DateTime.Now;
theMsgsTable.Rows.Add(theRow); // 将该行数据添加进表
theMsgsTable.AcceptChanges();
return true;
}
/// <summary>
/// 得到发给某人的短消息
/// </summary>
/// <param name="inp_GetUser_ID">某人的ID</param>
/// <returns>DataTable</returns>
public DataTable GetSomeBodyMsgs(string inp_GetUser_ID)
{
DataTable theBackTable = null;
if (inp_GetUser_ID != null && inp_GetUser_ID != "" && DAL.DBBase.IsInt(inp_GetUser_ID))
{
DataView theDV = new DataView(theMsgsTable);
theDV.AllowDelete = true;
theDV.AllowEdit = true;
theDV.AllowNew = true;
theDV.RowFilter = "GetUserID = " + inp_GetUser_ID;
theDV.Sort = "SendTime";
theBackTable = theDV.ToTable();//从视图中取值
for (int i = 0; i < theDV.Count; i++)
{
theDV.Delete(i);
}
theMsgsTable.AcceptChanges();
}
return theBackTable;
}
}