/// <summary> /// 新增字段 /// </summary> /// <returns></returns> public ActionResult AddFields() { try { UserEntity uInfo = ViewData["Account"] as UserEntity; FieldsEntity entityAdd = new FieldsEntity(); entityAdd.TabId = int.Parse(Request["TabId"]); entityAdd.FieldName = Request["FieldName"].Trim(); entityAdd.FieldViewName = Request["FieldViewName"].Trim(); entityAdd.FieldDataTypeId = int.Parse(Request["FieldDataTypeId"]); entityAdd.IsActive = bool.Parse(Request["IsActive"]); entityAdd.IsSearch = bool.Parse(Request["IsSearch"]); entityAdd.CreateBy = uInfo.AccountName; entityAdd.CreateTime = DateTime.Now; entityAdd.UpdateBy = uInfo.AccountName; entityAdd.UpdateTime = DateTime.Now; entityAdd.Sort = int.Parse(Request["Sort"]); bool ExistsFieldName = new FieldsBLL().ExistsFieldName(entityAdd.FieldName, entityAdd.TabId); bool ExistsFieldViewName = new FieldsBLL().ExistsFieldViewName(entityAdd.FieldViewName, entityAdd.TabId); if (ExistsFieldName) { return(Content("{\"msg\":\"添加失败,字段名已存在!\",\"success\":false}")); } else if (ExistsFieldViewName) { return(Content("{\"msg\":\"添加失败,字段显示名已存在!\",\"success\":false}")); } else { int entityId = new FieldsBLL().Add(entityAdd); if (entityId > 0) { //新增数据库表字段 获取表信息 TableEntity tabEntity = new TableBLL().GetModel(entityAdd.TabId); DataTypeEntity dataTypeEntity = new DataTypeBLL().GetModel(entityAdd.FieldDataTypeId); string dbTabName = "tb_" + tabEntity.TabName; if (Comm.AddTabField(dbTabName, entityAdd.FieldName, dataTypeEntity.DataType)) { return(Content("{\"msg\":\"添加成功!\",\"success\":true}")); } else { return(Content("{\"msg\":\"添加失败!\",\"success\":false}")); } } else { return(Content("{\"msg\":\"添加失败!\",\"success\":false}")); } } } catch (Exception ex) { return(Content("{\"msg\":\"添加失败," + ex.Message + "\",\"success\":false}")); } }
/// <summary> /// 更新一条数据 /// </summary> public int Update(FieldsEntity model) { StringBuilder strSql = new StringBuilder(); strSql.Append("update tbFields set "); strSql.Append(" UpdateTime = @UpdateTime , "); strSql.Append(" UpdateBy = @UpdateBy , "); strSql.Append(" TabId = @TabId , "); strSql.Append(" FieldName = @FieldName , "); strSql.Append(" FieldViewName = @FieldViewName , "); strSql.Append(" FieldDataTypeId = @FieldDataTypeId , "); strSql.Append(" IsActive = @IsActive , "); strSql.Append(" CreateTime = @CreateTime , "); strSql.Append(" CreateBy = @CreateBy , "); strSql.Append(" Sort = @Sort , "); strSql.Append(" IsSearch = @IsSearch "); strSql.Append(" where Id=@Id "); SqlParameter[] parameters = { new SqlParameter("@Id", SqlDbType.Int, 4), new SqlParameter("@UpdateTime", SqlDbType.DateTime), new SqlParameter("@UpdateBy", SqlDbType.NVarChar, 50), new SqlParameter("@TabId", SqlDbType.Int, 4), new SqlParameter("@FieldName", SqlDbType.NVarChar, 50), new SqlParameter("@FieldViewName", SqlDbType.NVarChar, 50), new SqlParameter("@FieldDataTypeId", SqlDbType.Int, 4), new SqlParameter("@IsActive", SqlDbType.Bit, 1), new SqlParameter("@CreateTime", SqlDbType.DateTime), new SqlParameter("@CreateBy", SqlDbType.NVarChar, 50), new SqlParameter("@Sort", SqlDbType.Int, 4), new SqlParameter("@IsSearch", SqlDbType.Bit, 1) }; parameters[0].Value = model.Id; parameters[1].Value = model.UpdateTime; parameters[2].Value = model.UpdateBy; parameters[3].Value = model.TabId; parameters[4].Value = model.FieldName; parameters[5].Value = model.FieldViewName; parameters[6].Value = model.FieldDataTypeId; parameters[7].Value = model.IsActive; parameters[8].Value = model.CreateTime; parameters[9].Value = model.CreateBy; parameters[10].Value = model.Sort; parameters[11].Value = model.IsSearch; object obj = SqlHelper.ExecuteNonQuery(SqlHelper.connStr, CommandType.Text, strSql.ToString(), parameters); return(Convert.ToInt32(obj)); }
/// <summary> /// 增加一条数据 /// </summary> public int Add(FieldsEntity model) { StringBuilder strSql = new StringBuilder(); strSql.Append("insert into tbFields("); strSql.Append("UpdateTime,UpdateBy,TabId,FieldName,FieldViewName,FieldDataTypeId,IsActive,CreateTime,CreateBy,Sort,IsSearch"); strSql.Append(") values ("); strSql.Append("@UpdateTime,@UpdateBy,@TabId,@FieldName,@FieldViewName,@FieldDataTypeId,@IsActive,@CreateTime,@CreateBy,@Sort,@IsSearch"); strSql.Append(") "); strSql.Append(";select @@IDENTITY"); SqlParameter[] parameters = { new SqlParameter("@UpdateTime", SqlDbType.DateTime), new SqlParameter("@UpdateBy", SqlDbType.NVarChar, 50), new SqlParameter("@TabId", SqlDbType.Int, 4), new SqlParameter("@FieldName", SqlDbType.NVarChar, 50), new SqlParameter("@FieldViewName", SqlDbType.NVarChar, 50), new SqlParameter("@FieldDataTypeId", SqlDbType.Int, 4), new SqlParameter("@IsActive", SqlDbType.Bit, 1), new SqlParameter("@CreateTime", SqlDbType.DateTime), new SqlParameter("@CreateBy", SqlDbType.NVarChar, 50), new SqlParameter("@Sort", SqlDbType.Int, 4), new SqlParameter("@IsSearch", SqlDbType.Bit, 1) }; parameters[0].Value = model.UpdateTime; parameters[1].Value = model.UpdateBy; parameters[2].Value = model.TabId; parameters[3].Value = model.FieldName; parameters[4].Value = model.FieldViewName; parameters[5].Value = model.FieldDataTypeId; parameters[6].Value = model.IsActive; parameters[7].Value = model.CreateTime; parameters[8].Value = model.CreateBy; parameters[9].Value = model.Sort; parameters[10].Value = model.IsSearch; object obj = Convert.ToInt32(SqlHelper.ExecuteScalar(SqlHelper.connStr, CommandType.Text, strSql.ToString(), parameters)); return(Convert.ToInt32(obj)); }
/// <summary> /// 更新一条数据 /// </summary> public int Update(FieldsEntity model) { return(dal.Update(model)); }
/// <summary> /// 增加一条数据 /// </summary> public int Add(FieldsEntity model) { return(dal.Add(model)); }
/// <summary> /// 获得数据列表 /// </summary> public List <FieldsEntity> DataTableToList(DataTable dt) { List <FieldsEntity> modelList = new List <FieldsEntity>(); int rowsCount = dt.Rows.Count; if (rowsCount > 0) { FieldsEntity model; for (int n = 0; n < rowsCount; n++) { model = new FieldsEntity(); if (dt.Rows[n]["Id"].ToString() != "") { model.Id = int.Parse(dt.Rows[n]["Id"].ToString()); } if (dt.Rows[n]["UpdateTime"].ToString() != "") { model.UpdateTime = DateTime.Parse(dt.Rows[n]["UpdateTime"].ToString()); } model.UpdateBy = dt.Rows[n]["UpdateBy"].ToString(); if (dt.Rows[n]["TabId"].ToString() != "") { model.TabId = int.Parse(dt.Rows[n]["TabId"].ToString()); } model.FieldName = dt.Rows[n]["FieldName"].ToString(); model.FieldViewName = dt.Rows[n]["FieldViewName"].ToString(); if (dt.Rows[n]["FieldDataTypeId"].ToString() != "") { model.FieldDataTypeId = int.Parse(dt.Rows[n]["FieldDataTypeId"].ToString()); } if (dt.Rows[n]["IsActive"].ToString() != "") { if ((dt.Rows[n]["IsActive"].ToString() == "1") || (dt.Rows[n]["IsActive"].ToString().ToLower() == "true")) { model.IsActive = true; } else { model.IsActive = false; } } if (dt.Rows[n]["IsSearch"].ToString() != "") { if ((dt.Rows[n]["IsSearch"].ToString() == "1") || (dt.Rows[n]["IsSearch"].ToString().ToLower() == "true")) { model.IsSearch = true; } else { model.IsSearch = false; } } if (dt.Rows[n]["Sort"].ToString() != "") { model.Sort = int.Parse(dt.Rows[n]["Sort"].ToString()); } if (dt.Rows[n]["CreateTime"].ToString() != "") { model.CreateTime = DateTime.Parse(dt.Rows[n]["CreateTime"].ToString()); } model.CreateBy = dt.Rows[n]["CreateBy"].ToString(); modelList.Add(model); } } return(modelList); }
/// <summary> /// 得到一个对象实体 /// </summary> public FieldsEntity GetModel(int Id) { StringBuilder strSql = new StringBuilder(); strSql.Append("select Id, UpdateTime, UpdateBy, TabId, FieldName, FieldViewName, FieldDataTypeId, Sort, IsActive, CreateTime, CreateBy ,IsSearch "); strSql.Append(" from tbFields "); strSql.Append(" where Id=@Id"); SqlParameter[] parameters = { new SqlParameter("@Id", SqlDbType.Int, 4) }; parameters[0].Value = Id; FieldsEntity model = new FieldsEntity(); 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()); } if (dt.Rows[0]["UpdateTime"].ToString() != "") { model.UpdateTime = DateTime.Parse(dt.Rows[0]["UpdateTime"].ToString()); } model.UpdateBy = dt.Rows[0]["UpdateBy"].ToString(); if (dt.Rows[0]["TabId"].ToString() != "") { model.TabId = int.Parse(dt.Rows[0]["TabId"].ToString()); } model.FieldName = dt.Rows[0]["FieldName"].ToString(); model.FieldViewName = dt.Rows[0]["FieldViewName"].ToString(); if (dt.Rows[0]["FieldDataTypeId"].ToString() != "") { model.FieldDataTypeId = int.Parse(dt.Rows[0]["FieldDataTypeId"].ToString()); } if (dt.Rows[0]["IsActive"].ToString() != "") { if ((dt.Rows[0]["IsActive"].ToString() == "1") || (dt.Rows[0]["IsActive"].ToString().ToLower() == "true")) { model.IsActive = true; } else { model.IsActive = false; } } if (dt.Rows[0]["IsSearch"].ToString() != "") { if ((dt.Rows[0]["IsSearch"].ToString() == "1") || (dt.Rows[0]["IsSearch"].ToString().ToLower() == "true")) { model.IsSearch = true; } else { model.IsSearch = false; } } if (dt.Rows[0]["Sort"].ToString() != "") { model.Sort = int.Parse(dt.Rows[0]["Sort"].ToString()); } if (dt.Rows[0]["CreateTime"].ToString() != "") { model.CreateTime = DateTime.Parse(dt.Rows[0]["CreateTime"].ToString()); } model.CreateBy = dt.Rows[0]["CreateBy"].ToString(); return(model); } else { return(null); } }