/// <summary> /// 增加一条数据 /// </summary> public int Add(HtmlTypeModel model) { StringBuilder strSql = new StringBuilder(); strSql.Append("insert into tbHtmlType("); strSql.Append("HtmlTypeName,Sort,CreateBy,CreateTime,UpdateBy,UpdateTime"); strSql.Append(") values ("); strSql.Append("@HtmlTypeName,@Sort,@CreateBy,@CreateTime,@UpdateBy,@UpdateTime"); strSql.Append(") "); strSql.Append(";select @@IDENTITY"); SqlParameter[] parameters = { new SqlParameter("@HtmlTypeName", SqlDbType.NVarChar, 50), new SqlParameter("@CreateBy", SqlDbType.NVarChar, 50), new SqlParameter("@CreateTime", SqlDbType.DateTime), new SqlParameter("@UpdateBy", SqlDbType.NVarChar, 50), new SqlParameter("@UpdateTime", SqlDbType.DateTime), new SqlParameter("@Sort", SqlDbType.Int) }; parameters[0].Value = model.HtmlTypeName; parameters[1].Value = model.CreateBy; parameters[2].Value = model.CreateTime; parameters[3].Value = model.UpdateBy; parameters[4].Value = model.UpdateTime; parameters[5].Value = model.Sort; object obj = Convert.ToInt32(SqlHelper.ExecuteScalar(SqlHelper.connStr, CommandType.Text, strSql.ToString(), parameters)); return(Convert.ToInt32(obj)); }
/// <summary> /// 新增 /// </summary> /// <returns></returns> public ActionResult AddHtmlType() { try { UserModel uInfo = ViewData["Account"] as UserModel; HtmlTypeModel typeAdd = new HtmlTypeModel(); typeAdd.HtmlTypeName = Request["HtmlTypeName"]; typeAdd.Sort = int.Parse(Request["Sort"]); typeAdd.CreateBy = uInfo.AccountName; typeAdd.CreateTime = DateTime.Now; typeAdd.UpdateBy = uInfo.AccountName; typeAdd.UpdateTime = DateTime.Now; bool exists = new HtmlTypeBLL().Exists(typeAdd.HtmlTypeName); if (exists) { return(Content("{\"msg\":\"添加失败,类型名称已存在!\",\"success\":false}")); } else { int typeId = new HtmlTypeBLL().Add(typeAdd); if (typeId > 0) { return(Content("{\"msg\":\"添加成功!\",\"success\":true}")); } else { return(Content("{\"msg\":\"添加失败!\",\"success\":false}")); } } } catch (Exception ex) { return(Content("{\"msg\":\"添加失败," + ex.Message + "\",\"success\":false}")); } }
/// <summary> /// 得到一个对象实体 /// </summary> public HtmlTypeModel GetModel(int id) { StringBuilder strSql = new StringBuilder(); strSql.Append("select id, HtmlTypeName, Sort, CreateTime,CreateBy,UpdateTime,UpdateBy "); strSql.Append(" from tbHtmlType "); strSql.Append(" where id=@id"); SqlParameter[] parameters = { new SqlParameter("@id", SqlDbType.Int, 4) }; parameters[0].Value = id; HtmlTypeModel model = new HtmlTypeModel(); DataTable dt = SqlHelper.GetDataTable(SqlHelper.connStr, CommandType.Text, strSql.ToString(), parameters); if (dt.Rows.Count > 0) { if (dt.Rows[0]["id"].ToString() != "") { model.id = int.Parse(dt.Rows[0]["id"].ToString()); } model.HtmlTypeName = dt.Rows[0]["HtmlTypeName"].ToString(); if (dt.Rows[0]["Sort"].ToString() != "") { model.Sort = int.Parse(dt.Rows[0]["Sort"].ToString()); } model.CreateBy = dt.Rows[0]["CreateBy"].ToString(); if (dt.Rows[0]["CreateTime"].ToString() != "") { model.CreateTime = DateTime.Parse(dt.Rows[0]["CreateTime"].ToString()); } model.UpdateBy = dt.Rows[0]["UpdateBy"].ToString(); if (dt.Rows[0]["UpdateTime"].ToString() != "") { model.UpdateTime = DateTime.Parse(dt.Rows[0]["UpdateTime"].ToString()); } return(model); } else { return(null); } }
/// <summary> /// 更新一条数据 /// </summary> public int Update(HtmlTypeModel model) { StringBuilder strSql = new StringBuilder(); strSql.Append("update tbHtmlType set "); strSql.Append(" HtmlTypeName = @HtmlTypeName , "); strSql.Append(" Sort = @Sort , "); strSql.Append(" CreateBy = @CreateBy , "); strSql.Append(" CreateTime = @CreateTime , "); strSql.Append(" UpdateBy = @UpdateBy , "); strSql.Append(" UpdateTime = @UpdateTime "); strSql.Append(" where id=@id "); SqlParameter[] parameters = { new SqlParameter("@id", SqlDbType.Int, 4), new SqlParameter("@HtmlTypeName", SqlDbType.NVarChar, 50), new SqlParameter("@CreateBy", SqlDbType.NVarChar, 50), new SqlParameter("@CreateTime", SqlDbType.DateTime), new SqlParameter("@UpdateBy", SqlDbType.NVarChar, 50), new SqlParameter("@UpdateTime", SqlDbType.DateTime), new SqlParameter("@Sort", SqlDbType.Int) }; parameters[0].Value = model.id; parameters[1].Value = model.HtmlTypeName; parameters[2].Value = model.CreateBy; parameters[3].Value = model.CreateTime; parameters[4].Value = model.UpdateBy; parameters[5].Value = model.UpdateTime; parameters[6].Value = model.Sort; object obj = SqlHelper.ExecuteNonQuery(SqlHelper.connStr, CommandType.Text, strSql.ToString(), parameters); return(Convert.ToInt32(obj)); }