public JObject save(HttpRequest blogForm) { bool result = false; string msg = ""; if (blogForm != null) { Blog blog = new Blog(); base.CopyProperties(blog, blogForm); try { blog.CommitTime = DateTime.Now; blog.UpdateTime = DateTime.Now; db.Blog.Add(blog); db.SaveChanges(); msg = "保存成功!"; result = true; } catch (Exception error) { msg = "操作失败:" + error.Message + ",请重试!"; } } return new JObject( new JProperty("success", result), new JProperty("msg", msg) ); }
/// <summary> /// 导入:博客 /// </summary> public static JObject importBlog(string fileName) { //Excel导出入到DataTable DataTable dt = UtilExcelOle.ExcelToDataTableBySheet(fileName, "Blog"); if (dt != null) { Dictionary<string, string> dic = new Dictionary<string, string>() { {"标识","ID"}, {"用户名称","Username"}, {"博客标题","Blog_Name"}, {"博客内容","Blog_Content"}, {"创建时间","CommitTime"}, {"更新时间","UpdateTime"} }; UtilDataTable.ReplaceColumnName(dt, dic); //循环插入数据 for (int i = 0; i < dt.Rows.Count; i++) { Blog blog = new Blog(); UtilDataTable.ToObject(blog, dt.Rows[i], dt.Columns); User user = db.User.Where(e => e.Username.Equals(blog.Username)).SingleOrDefault(); blog.User_ID = user.ID; db.Blog.Add(blog); } db.SaveChanges(); } return new JObject( new JProperty("success", true), new JProperty("data", true) ); }