コード例 #1
0
 public JObject save(HttpRequest commentForm)
 {
     bool result = false;
     string msg = "";
     if (commentForm != null)
     {
         Comment comment = new Comment();
         base.CopyProperties(comment, commentForm);
         try
         {
             comment.Comment1 = comment.Content;
             comment.CommitTime = DateTime.Now;
             comment.UpdateTime = DateTime.Now;
             db.Comment.Add(comment);
             db.SaveChanges();
             msg = "保存成功!";
             result = true;
         }
         catch (Exception error)
         {
             msg = "操作失败:" + error.Message + ",请重试!";
         }
     }
     return new JObject(
         new JProperty("success", result),
         new JProperty("msg", msg)
     );
 }
コード例 #2
0
        /// <summary>
        /// 导入:评论
        /// </summary>
        public static JObject importComment(string fileName)
        {
            //Excel导出入到DataTable
            DataTable dt = UtilExcelOle.ExcelToDataTableBySheet(fileName, "Comment");
            if (dt != null)
            {
                Dictionary<string, string> dic = new Dictionary<string, string>()
                {
                    {"标识","ID"},
                    {"评论者","Username"},
                    {"评论","Content"},
                    {"博客标题","Blog_Name"},
                    {"创建时间","CommitTime"},
                    {"更新时间","UpdateTime"}
                };
                UtilDataTable.ReplaceColumnName(dt, dic);

                //循环插入数据
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    Comment comment = new Comment();
                    UtilDataTable.ToObject(comment, dt.Rows[i], dt.Columns);

                    User user = db.User.Where(e => e.Username.Equals(comment.Username)).SingleOrDefault();
                    comment.User_ID = user.ID;
                    comment.Comment1 = comment.Content;
                    Blog blog = db.Blog.Where(e => e.Blog_Name.Equals(comment.Blog_Name)).SingleOrDefault();
                    comment.Blog_ID = blog.ID;
                    db.Comment.Add(comment);
                }
                db.SaveChanges();
            }
            return new JObject(
                new JProperty("success", true),
                new JProperty("data", true)
            );
        }