/// <summary> /// 所有插入消息的状态都是未读消息 /// </summary> /// <param name="item"></param> /// <returns></returns> public int InsertMessage(MessageItem item) { DbOperate dbo = new DbOperate(); dbo.TableName = MessageOperator.TABLENAME; SqlCommand com = new SqlCommand(); com.CommandText = "insert into Messages values(@fromUserId,@toUserId,@message,@messageTypeId,@messageState,@messageTime)"; com.Parameters.Add(new SqlParameter("@fromUserId", SqlDbType.Int)).Value = item.FromUserId; com.Parameters.Add(new SqlParameter("@toUserId", SqlDbType.Int)).Value = item.ToUserId; com.Parameters.Add(new SqlParameter("@message", SqlDbType.VarChar,50)).Value = item.Message; com.Parameters.Add(new SqlParameter("@messageTypeId", SqlDbType.Int)).Value = item.MessageTypeId; com.Parameters.Add(new SqlParameter("@messageState", SqlDbType.Int)).Value = item.MessageState; com.Parameters.Add(new SqlParameter("@messageTime", SqlDbType.DateTime)).Value = item.MessageTime; return dbo.Insert(com); }
/// <summary> /// 获取一定事件范围内,特定好友的聊天记录 /// </summary> /// <param name="hostid"></param> /// <param name="friendId"></param> /// <param name="time"></param> /// <returns></returns> public MessageList GetWeMsg(int hostid, int friendId, DateTime time) { DbOperate dbo = new DbOperate(); dbo.TableName = MessageOperator.TABLENAME; MessageList returnList = new MessageList(); SqlCommand com = new SqlCommand(); com.CommandText = "select * from Messages where toUserId=@hostID and fromUSerid=@friendId and MessageState=0 and MessageTime between @startTime and @endTime"; SqlParameter hostPar = new SqlParameter("@hostID",SqlDbType.Int); hostPar.Value = hostid; SqlParameter friendPar = new SqlParameter("@friendId",SqlDbType.Int); friendPar.Value = friendId; SqlParameter start = new SqlParameter("@startTime",SqlDbType.DateTime); start.Value = time.AddSeconds(-2); SqlParameter end = new SqlParameter("@endTime",SqlDbType.DateTime); end.Value = time.AddSeconds(1); com.Parameters.Add(hostPar); com.Parameters.Add(friendPar); com.Parameters.Add(start); com.Parameters.Add(end); DataSet ds = dbo.Select(com); foreach (DataRow row in ds.Tables[0].Rows) { MessageItem item = new MessageItem(); item.Id = Convert.ToInt32(row["Id"]); item.FromUserId = Convert.ToInt32(row["FromUserId"]); item.ToUserId = hostid; item.Message = row["Message"].ToString(); item.MessageTypeId = Convert.ToInt32(row["MessageTypeId"]); item.MessageState = (MessageState)Convert.ToInt32(row["MessageState"]); item.MessageTime = Convert.ToDateTime(row["MessageTime"]); returnList.Add(item); } return returnList; }
/// <summary> /// 返回某用户的好友列表对象 /// </summary> /// <param name="fromUserId">用户的MyQQ号</param> /// <returns></returns> public MessageList GetOwnerMsgByFromUser(int fromUserId) { DbOperate dbo = new DbOperate(); dbo.TableName = MessageOperator.TABLENAME; SqlCommand com = new SqlCommand(); com.CommandText = "select * from Messages where fromUserId=@fromUserId"; SqlParameter fUserId = new SqlParameter("@fromUserId",SqlDbType.Int); fUserId.Value = fromUserId; dbo.TableName = MessageOperator.TABLENAME; com.Parameters.Add(fUserId); DataSet ds = dbo.Select(com); MessageList list = new MessageList(); foreach (DataRow row in ds.Tables[0].Rows) { MessageItem item = new MessageItem(); item.Id = Convert.ToInt32(row["Id"]); item.FromUserId = Convert.ToInt32(row["FromUserId"]); item.ToUserId =Convert.ToInt32(row["TouserId"]); item.Message = row["Message"].ToString(); item.MessageTypeId = Convert.ToInt32(row["MessageTypeId"]); item.MessageState = (MessageState)Convert.ToInt32(row["MessageState"]); item.MessageTime = Convert.ToDateTime(row["MessageTime"]); list.Add(item); } return list; }
public MessageItem GetMessage(int itemid) { DbOperate dbo = new DbOperate(); dbo.TableName = MessageOperator.TABLENAME; SqlCommand com = new SqlCommand(); com.CommandText = "select * from Messages where id=@MessageId"; SqlParameter Idpar = new SqlParameter("@MessageId", SqlDbType.Int); Idpar.Value = itemid; com.Parameters.Add(Idpar); DataSet ds = dbo.Select(com); if(ds.Tables[0].Rows.Count>0) { DataRow row = ds.Tables[0].Rows[0]; MessageItem item = new MessageItem(); item.Id = Convert.ToInt32(row["Id"]); item.FromUserId = Convert.ToInt32(row["FromUserId"]); item.ToUserId = Convert.ToInt32(row["ToUserId"]); item.Message = row["Message"].ToString(); item.MessageTypeId = Convert.ToInt32(row["MessageTypeId"]); item.MessageState = (MessageState)Convert.ToInt32(row["MessageState"]); item.MessageTime = Convert.ToDateTime(row["MessageTime"]); return item; } return null; }
/// <summary> /// 从消息列表中移除一个消息实体对象 /// </summary> /// <param name="item">消息实体对象</param> public void Remove(MessageItem item) { this.list.Remove(item); }
/// <summary> /// 往消息列表中增加一个消息对象 /// </summary> /// <param name="item">消息实体对象</param> /// <returns>消息实体对象在消息列表中的下标</returns> public int Add(MessageItem item) { return this.list.Add(item); }