/// <summary> /// 根据ID查询消息 /// </summary> /// <param name="message"></param> /// <returns></returns> public static Message GetMessageById(int messageId) { int typeId = 0; string sql = "select * from Message where MessageId=" + messageId; using (SqlDataReader reader = DBHelper.GetReader(sql)) { if (reader.Read()) { Message message = new Message(); message.MessageId = (int)reader["MessageId"]; message.Title = (string)reader["Title"]; message.Content = (string)reader["Content"]; typeId = (int)reader["Type"]; //再找MessageTypeService的根据Id 找MessageType message.BeginTime = (DateTime)reader["BeginTime"]; message.EndTime = (DateTime)reader["EndTime"]; message.RecordTime = (DateTime)reader["RecordTime"]; message.IfPublish = (int)reader["IfPublish"]; message.IfDelete = (int)reader["IfDelete"]; message.IfDeleteTo = (int)reader["IfDeleteTo"]; string userId = (string)reader["FromUserId"]; reader.Close(); message.FromUser = UserService.GetUserById(userId); message.Type = MessageTypeService.GetMessageTypeById(typeId); return(message); } else { reader.Close(); return(null); } } }
public static IList <Message> GetMessageBySql(string sql) { IList <Message> list = new List <Message>(); try { using (DataTable dt = DBHelper.GetDataSet(sql)) { foreach (DataRow row in dt.Rows) { Message message = new Message(); message.MessageId = (int)row["MessageId"]; message.Title = (string)row["Title"]; message.Content = (string)row["Content"]; message.Type = MessageTypeService.GetMessageTypeById((int)row["Type"]); message.BeginTime = (DateTime)row["BeginTime"]; message.EndTime = (DateTime)row["EndTime"]; message.RecordTime = (DateTime)row["RecordTime"]; message.IfPublish = (int)row["IfPublish"]; message.IfDelete = (int)row["IfDelete"]; message.IfDeleteTo = (int)row["IfDeleteTo"]; string userId = (string)row["FromUserId"]; message.FromUser = UserService.GetUserById(userId); list.Add(message); } return(list); } } catch (Exception ex) { throw new ApplicationException(ex.Message); } }