/// <summary> /// 向数据库中添加消息 /// </summary> /// <param name="m">消息</param> /// <returns>是否成功</returns> public bool addMsg(Msg m) { string cmdString = @"INSERT INTO msg (sessionId, senderId, receiverId, senderName, type, content, isComing,date, isRead, bak1, bak2, bak3, bak4, bak5, bak6)" + " VALUES (" + m.SessionId + "," + m.FromUserId + "," + m.ToUserId + "," + "'" + m.FromUserName + "'" + "," + m.Type + "," + "'" + m.Content + "'" + "," + m.IsComing + "," + "'" + m.Date + "'" + "," + m.IsReaded + "," + "'" + m.Bak1 + "'" + "," + "'" + m.Bak2 + "'" + "," + "'" + m.Bak3 + "'" + "," + "'" + m.Bak4 + "'" + "," + "'" + m.Bak5 + "'" + "," + "'" + m.Bak6 + "'" + ");"; conn.Open(); SQLiteCommand sqlAddMsg = new SQLiteCommand(cmdString, conn); sqlAddMsg.ExecuteNonQuery(); sqlAddMsg.Dispose(); conn.Close(); return true; }
/// <summary> /// 向数据库中添加消息 /// </summary> /// <param name="m">消息</param> /// <returns>是否成功</returns> public bool addMsg(Msg m) { string cmdString = @"INSERT TO msg VALUES (" + m.MsgId + "," + m.SessionId + "," + m.FromUser + "," + m.ToUser + "," + m.FromUserName + "," + m.Type + "," + m.Content + "," + m.IsComing + "," + m.Date + "," + "," + m.IsReaded + "," + m.Bak1 + "," + m.Bak2 + "," + m.Bak3 + "," + m.Bak4 + "," + m.Bak5 + "," + m.Bak6 + "," + ");"; SQLiteCommand sqlAddMsg = new SQLiteCommand(cmdString, conn); sqlAddMsg.ExecuteNonQuery(); sqlAddMsg.Dispose(); return true; }
//读取两者之间的所有消息 public List<Msg> readMsg(string senderId, string receiverId) { string cmdString = "SELECT * FROM msg where senderId =" + senderId + " and receiverId=" + receiverId + " or senderId = " + receiverId + " and receiverId=" + senderId + ";"; conn.Open(); SQLiteCommand sql = new SQLiteCommand(cmdString, conn); SQLiteDataReader result = sql.ExecuteReader(); List<Msg> a = new List<Msg>(); while(result.Read()) { string[] msgs = new string[] { result[3].ToString(), result[2].ToString(), result[6].ToString(), result[8].ToString(), result[4].ToString() }; Msg aMsg = new Msg(msgs, 1, 1); a.Add(aMsg); } result.Close(); sql.CommandText = "UPDATE msg SET isRead = 1 WHERE isRead = 0 AND senderId = " + senderId + ";"; sql.ExecuteNonQuery(); sql.Dispose(); conn.Close(); return a; }