public ResponseResult Save(Model.Notifications notification) { int count = dbContext.Create(notification); if (count > 0) { return(new SuccessResult("Save successful")); } else { return(new ErrorResult("Failed to save")); } }
public ActionResult Save() { string title = Request.Form["title"]; string position = Request.Form["position"]; string content = Request.Form["content"]; Model.Notifications notification = new Model.Notifications() { title = title, position = position, content = content, updateTime = DateTime.Now, createTime = DateTime.Now }; return(Json(notiService.Save(notification), JsonRequestBehavior.AllowGet)); }
public List <Model.Notifications> Get(string position) { List <Model.Notifications> notiList = new List <Model.Notifications>(); using (SqlConnection conn = new SqlConnection()) { string connectStr = ConfigurationManager.ConnectionStrings["connect"].ToString(); conn.ConnectionString = connectStr; SqlCommand cmd = new SqlCommand(); var pos = position.Split(',').Select((str) => { return("'" + str + "'"); }); var distStr = "(" + string.Join(",", pos) + ")"; cmd.CommandText = "select * from Notifications where is_del = 0 and position in " + distStr + " order by create_time desc"; cmd.Connection = conn; conn.Open(); SqlDataReader read = cmd.ExecuteReader(); while (read.Read()) { Model.Notifications noti = new Model.Notifications() { id = read.GetInt32(0), title = read.GetString(1), position = read.GetString(2), content = read.GetString(3), createTime = read.GetDateTime(4), updateTime = read.GetDateTime(5), isDel = read.GetBoolean(6) }; notiList.Add(noti); } conn.Close(); } return(notiList); }
public List <Model.Notifications> GetAll() { List <Model.Notifications> notiList = new List <Model.Notifications>(); using (SqlConnection conn = new SqlConnection()) { string connectStr = ConfigurationManager.ConnectionStrings["connect"].ToString(); conn.ConnectionString = connectStr; SqlCommand cmd = new SqlCommand(); cmd.CommandText = "select * from Notifications where is_del=0 order by create_time desc"; cmd.Connection = conn; conn.Open(); SqlDataReader read = cmd.ExecuteReader(); while (read.Read()) { Model.Notifications notification = new Model.Notifications() { id = read.GetInt32(0), title = read.GetString(1), position = read.GetString(2), content = read.GetString(3), createTime = read.GetDateTime(4), updateTime = read.GetDateTime(5), isDel = read.GetBoolean(6) }; notiList.Add(notification); } conn.Close(); } return(notiList); }
public int Create(Model.Notifications notification) { int rows = 0; using (SqlConnection conn = new SqlConnection()) { string connectStr = ConfigurationManager.ConnectionStrings["connect"].ToString(); conn.ConnectionString = connectStr; SqlCommand cmd = new SqlCommand(); cmd.CommandText = "insert into Notifications(title, position, content, create_time, update_time) values(@title, @position, @content, @createTime, @updateTime)"; cmd.Parameters.AddWithValue("@title", notification.title); cmd.Parameters.AddWithValue("@position", notification.position); cmd.Parameters.AddWithValue("@content", notification.content); cmd.Parameters.AddWithValue("@createTime", notification.createTime); cmd.Parameters.AddWithValue("@updateTime", notification.updateTime); cmd.Connection = conn; conn.Open(); rows = cmd.ExecuteNonQuery(); conn.Close(); } return(rows); }