/// <summary> /// 插入一条记录到表DicEnum,如果表中存在自增字段,则返回值为新记录的自增字段值,否则返回0 /// </summary> /// <param name="db">数据库操作对象</param> /// <param name="dicEnum">待插入的实体对象</param> public int AddDicEnum(DicEnumEntity entity) { string sql = @"insert into DicEnum( [Code],[Name],[Description],[ParentCode],[ParentId],[ParentId1],[ParentId2],[ParentId3],[IsActive],[CreateTime],[CreateMan],[CanEdit])VALUES ( @code,@name,@description,@parentCode,@parentId,@parentId1,@parentId2,@parentId3,@isActive,@createTime,@createMan,@canEdit); SELECT SCOPE_IDENTITY();" ; DbCommand cmd = db.GetSqlStringCommand(sql); db.AddInParameter(cmd, "@Code", DbType.String, entity.Code); db.AddInParameter(cmd, "@Name", DbType.String, entity.Name); db.AddInParameter(cmd, "@Description", DbType.String, entity.Description); db.AddInParameter(cmd, "@ParentCode", DbType.String, entity.ParentCode); db.AddInParameter(cmd, "@ParentId", DbType.Int32, entity.ParentId); db.AddInParameter(cmd, "@ParentId1", DbType.Int32, entity.ParentId1); db.AddInParameter(cmd, "@ParentId2", DbType.Int32, entity.ParentId2); db.AddInParameter(cmd, "@ParentId3", DbType.Int32, entity.ParentId3); db.AddInParameter(cmd, "@IsActive", DbType.Int32, entity.IsActive); db.AddInParameter(cmd, "@CreateTime", DbType.DateTime, entity.CreateTime); db.AddInParameter(cmd, "@CreateMan", DbType.Int32, entity.CreateMan); db.AddInParameter(cmd, "@CanEdit", DbType.Int32, entity.CanEdit); object identity = db.ExecuteScalar(cmd); if (identity == null || identity == DBNull.Value) { return(0); } return(Convert.ToInt32(identity)); }
/// <summary> /// 读取记录列表。 /// </summary> /// <param name="db">数据库操作对象</param> /// <param name="columns">需要返回的列,不提供任何列名时默认将返回所有列</param> public IList <DicEnumEntity> GetDicEnumAll() { string sql = @"SELECT [Id],[Code],[Name],[Description],[ParentCode],[ParentId],[ParentId1],[ParentId2],[ParentId3],[IsActive],[CreateTime],[CreateMan],[CanEdit] from dbo.[DicEnum] WITH(NOLOCK) "; IList <DicEnumEntity> entityList = new List <DicEnumEntity>(); DbCommand cmd = db.GetSqlStringCommand(sql); using (IDataReader reader = db.ExecuteReader(cmd)) { while (reader.Read()) { DicEnumEntity entity = new DicEnumEntity(); entity.Id = StringUtils.GetDbInt(reader["Id"]); entity.Code = StringUtils.GetDbString(reader["Code"]); entity.Name = StringUtils.GetDbString(reader["Name"]); entity.Description = StringUtils.GetDbString(reader["Description"]); entity.ParentCode = StringUtils.GetDbString(reader["ParentCode"]); entity.ParentId = StringUtils.GetDbInt(reader["ParentId"]); entity.ParentId1 = StringUtils.GetDbInt(reader["ParentId1"]); entity.ParentId2 = StringUtils.GetDbInt(reader["ParentId2"]); entity.ParentId3 = StringUtils.GetDbInt(reader["ParentId3"]); entity.IsActive = StringUtils.GetDbInt(reader["IsActive"]); entity.CreateTime = StringUtils.GetDbDateTime(reader["CreateTime"]); entity.CreateMan = StringUtils.GetDbInt(reader["CreateMan"]); entity.CanEdit = StringUtils.GetDbInt(reader["CanEdit"]); entityList.Add(entity); } } return(entityList); }
/// <summary> /// 根据主键值读取记录。如果数据库不存在这条数据将返回null /// </summary> /// <param name="db">数据库操作对象</param> /// <param name="columns">需要返回的列,不提供任何列名时默认将返回所有列</param> public DicEnumEntity GetDicEnum(int id) { string sql = @"SELECT [Id],[Code],[Name],[Description],[ParentCode],[ParentId],[ParentId1],[ParentId2],[ParentId3],[IsActive],[CreateTime],[CreateMan],[CanEdit] FROM dbo.[DicEnum] WITH(NOLOCK) WHERE [Id]=@id" ; DbCommand cmd = db.GetSqlStringCommand(sql); db.AddInParameter(cmd, "@Id", DbType.Int32, id); DicEnumEntity entity = new DicEnumEntity(); using (IDataReader reader = db.ExecuteReader(cmd)) { if (reader.Read()) { entity.Id = StringUtils.GetDbInt(reader["Id"]); entity.Code = StringUtils.GetDbString(reader["Code"]); entity.Name = StringUtils.GetDbString(reader["Name"]); entity.Description = StringUtils.GetDbString(reader["Description"]); entity.ParentCode = StringUtils.GetDbString(reader["ParentCode"]); entity.ParentId = StringUtils.GetDbInt(reader["ParentId"]); entity.ParentId1 = StringUtils.GetDbInt(reader["ParentId1"]); entity.ParentId2 = StringUtils.GetDbInt(reader["ParentId2"]); entity.ParentId3 = StringUtils.GetDbInt(reader["ParentId3"]); entity.IsActive = StringUtils.GetDbInt(reader["IsActive"]); entity.CreateTime = StringUtils.GetDbDateTime(reader["CreateTime"]); entity.CreateMan = StringUtils.GetDbInt(reader["CreateMan"]); entity.CanEdit = StringUtils.GetDbInt(reader["CanEdit"]); } } return(entity); }
/// <summary> /// 更新一条DicEnum记录。 /// 该方法提供给界面等UI层调用 /// </summary> /// <param name="dicEnum">待更新的实体对象</param> /// <param name="columns">要更新的列名,不提供任何列名时默认将更新主键之外的所有列</param> public int UpdateDicEnum(DicEnumEntity dicEnum) { if (string.IsNullOrEmpty(dicEnum.Name) || dicEnum.Id == 0) { return((int)CommonStatus.Update_Fail_Empty); } else if (DicEnumBLL.Instance.IsExist(dicEnum)) { return((int)CommonStatus.Update_Fail_Exist); } return(DicEnumDA.Instance.UpdateDicEnum(dicEnum)); }
/// <summary> /// 判断当前节点是否已存在相同的 /// </summary> /// <param name="entity"></param> /// <returns></returns> public int ExistNum(DicEnumEntity entity) { ///id=0,判断总数,ID>0判断除自己之外的总数 string sql = @"Select count(1) from dbo.[DicEnum] WITH(NOLOCK) "; string where = "where "; if (entity.Id == 0) { where = where + " (Name=@Name and ParentId=@ParentId) "; if (!string.IsNullOrEmpty(entity.Code)) { where = where + " or Code=@Code "; } } else { where = where + " id<>@Id and ((Name=@Name and ParentId=@ParentId) "; if (!string.IsNullOrEmpty(entity.Code)) { where = where + " or Code=@Code) "; } else { where = where + ") "; } } sql = sql + where; DbCommand cmd = db.GetSqlStringCommand(sql); if (!string.IsNullOrEmpty(entity.Code)) { db.AddInParameter(cmd, "@Code", DbType.String, entity.Code); } if (entity.Id > 0) { db.AddInParameter(cmd, "@Id", DbType.Int32, entity.Id); } db.AddInParameter(cmd, "@Name", DbType.String, entity.Name); db.AddInParameter(cmd, "@ParentId", DbType.Int32, entity.ParentId); object identity = db.ExecuteScalar(cmd); if (identity == null || identity == DBNull.Value) { return(0); } return(Convert.ToInt32(identity)); }
/// <summary> /// 插入一条记录到表DicEnum,返回操作数。 /// 该方法提供给界面等UI层调用 /// </summary> /// <param name="dicEnum">要添加的DicEnum数据实体对象</param> public int AddDicEnum(DicEnumEntity dicEnum) { if (dicEnum.Id > 0) { return(UpdateDicEnum(dicEnum)); } else if (string.IsNullOrEmpty(dicEnum.Name)) { return((int)CommonStatus.ADD_Fail_Empty); } else if (DicEnumBLL.Instance.IsExist(dicEnum)) { return((int)CommonStatus.ADD_Fail_Exist); } else { return(DicEnumDA.Instance.AddDicEnum(dicEnum)); } }
/// <summary> /// 根据主键值更新记录的全部字段(注意:该方法不会对自增字段、timestamp类型字段以及主键字段更新!如果要更新主键字段,请使用Update方法)。 /// 如果数据库有数据被更新了则返回True,否则返回False /// </summary> /// <param name="db">数据库操作对象</param> /// <param name="dicEnum">待更新的实体对象</param> public int UpdateDicEnum(DicEnumEntity entity) { string sql = @" UPDATE dbo.[DicEnum] SET [Code]=@code,[Name]=@name,[Description]=@description,[ParentCode]=@parentCode,[ParentId]=@parentId,[ParentId1]=@parentId1,[ParentId2]=@parentId2,[ParentId3]=@parentId3,[IsActive]=@isActive,[CreateTime]=@createTime,[CreateMan]=@createMan,[CanEdit]=@canEdit WHERE [Id]=@id"; DbCommand cmd = db.GetSqlStringCommand(sql); db.AddInParameter(cmd, "@Id", DbType.Int32, entity.Id); db.AddInParameter(cmd, "@Code", DbType.String, entity.Code); db.AddInParameter(cmd, "@Name", DbType.String, entity.Name); db.AddInParameter(cmd, "@Description", DbType.String, entity.Description); db.AddInParameter(cmd, "@ParentCode", DbType.String, entity.ParentCode); db.AddInParameter(cmd, "@ParentId", DbType.Int32, entity.ParentId); db.AddInParameter(cmd, "@ParentId1", DbType.Int32, entity.ParentId1); db.AddInParameter(cmd, "@ParentId2", DbType.Int32, entity.ParentId2); db.AddInParameter(cmd, "@ParentId3", DbType.Int32, entity.ParentId3); db.AddInParameter(cmd, "@IsActive", DbType.Int32, entity.IsActive); db.AddInParameter(cmd, "@CreateTime", DbType.DateTime, entity.CreateTime); db.AddInParameter(cmd, "@CreateMan", DbType.Int32, entity.CreateMan); db.AddInParameter(cmd, "@CanEdit", DbType.Int32, entity.CanEdit); return(db.ExecuteNonQuery(cmd)); }
/// <summary> /// 判断对象是否存在 /// </summary> /// <param name="dicEnum"></param> /// <returns></returns> public bool IsExist(DicEnumEntity dicEnum) { return(DicEnumDA.Instance.ExistNum(dicEnum) > 0); }
/// <summary> /// 读取记录列表。 /// </summary> /// <param name="db">数据库操作对象</param> /// <param name="columns">需要返回的列,不提供任何列名时默认将返回所有列</param> public IList <DicEnumEntity> GetDicEnumList(int pagesize, int pageindex, ref int recordCount, string keyword, int pid) { string where = "where 1=1 "; if (!string.IsNullOrEmpty(keyword)) { where += "and (Name Like @Keyword or Code Like @Keyword) "; } if (pid == CommonKey.RootParent || pid > 0) { where += "and ParentId=@ParentId "; } string sql = @"SELECT [Id],[Code],[Name],[Description],[ParentCode],[ParentId],[ParentId1],[ParentId2],[ParentId3],[IsActive],[CreateTime],[CreateMan],[CanEdit] FROM (SELECT ROW_NUMBER() OVER (ORDER BY Id desc) AS ROWNUMBER, [Id],[Code],[Name],[Description],[ParentCode],[ParentId],[ParentId1],[ParentId2],[ParentId3],[IsActive],[CreateTime],[CreateMan],[CanEdit] from dbo.[DicEnum] WITH(NOLOCK) " + where + @") as temp where rownumber BETWEEN ((@PageIndex - 1) * @PageSize + 1) AND @PageIndex * @PageSize" ; string sql2 = @"Select count(1) from dbo.[DicEnum] with (nolock) " + where; IList <DicEnumEntity> entityList = new List <DicEnumEntity>(); DbCommand cmd = db.GetSqlStringCommand(sql); db.AddInParameter(cmd, "@PageIndex", DbType.Int32, pageindex); db.AddInParameter(cmd, "@PageSize", DbType.Int32, pagesize); if (!string.IsNullOrEmpty(keyword)) { db.AddInParameter(cmd, "@Keyword", DbType.String, "%" + keyword + "%"); } if (pid == CommonKey.RootParent || pid > 0) { db.AddInParameter(cmd, "@ParentId", DbType.Int32, pid); } using (IDataReader reader = db.ExecuteReader(cmd)) { while (reader.Read()) { DicEnumEntity entity = new DicEnumEntity(); entity.Id = StringUtils.GetDbInt(reader["Id"]); entity.Code = StringUtils.GetDbString(reader["Code"]); entity.Name = StringUtils.GetDbString(reader["Name"]); entity.Description = StringUtils.GetDbString(reader["Description"]); entity.ParentCode = StringUtils.GetDbString(reader["ParentCode"]); entity.ParentId = StringUtils.GetDbInt(reader["ParentId"]); entity.ParentId1 = StringUtils.GetDbInt(reader["ParentId1"]); entity.ParentId2 = StringUtils.GetDbInt(reader["ParentId2"]); entity.ParentId3 = StringUtils.GetDbInt(reader["ParentId3"]); entity.IsActive = StringUtils.GetDbInt(reader["IsActive"]); entity.CreateTime = StringUtils.GetDbDateTime(reader["CreateTime"]); entity.CreateMan = StringUtils.GetDbInt(reader["CreateMan"]); entity.CanEdit = StringUtils.GetDbInt(reader["CanEdit"]); entityList.Add(entity); } } cmd = db.GetSqlStringCommand(sql2); if (!string.IsNullOrEmpty(keyword)) { db.AddInParameter(cmd, "@Keyword", DbType.String, "%" + keyword + "%"); } if (pid >= 0) { db.AddInParameter(cmd, "@ParentId", DbType.Int32, pid); } using (IDataReader reader = db.ExecuteReader(cmd)) { if (reader.Read()) { recordCount = StringUtils.GetDbInt(reader[0]); } else { recordCount = 0; } } return(entityList); }