/// <summary> /// 根据主键值读取记录。如果数据库不存在这条数据将返回null /// </summary> /// <param name="db">数据库操作对象</param> /// <param name="columns">需要返回的列,不提供任何列名时默认将返回所有列</param> public MemFavoritesEntity GetMemFavorites(int id) { string sql = @"SELECT [Id],[ProductDetailId],[MemId],[CreateTime],[IsActive],[SystemId] FROM dbo.[MemFavorites] WITH(NOLOCK) WHERE [Id]=@id" ; DbCommand cmd = db.GetSqlStringCommand(sql); db.AddInParameter(cmd, "@Id", DbType.Int32, id); MemFavoritesEntity entity = new MemFavoritesEntity(); using (IDataReader reader = db.ExecuteReader(cmd)) { if (reader.Read()) { entity.Id = StringUtils.GetDbInt(reader["Id"]); entity.ProductDetailId = StringUtils.GetDbInt(reader["ProductDetailId"]); entity.MemId = StringUtils.GetDbInt(reader["MemId"]); entity.CreateTime = StringUtils.GetDbDateTime(reader["CreateTime"]); entity.IsActive = StringUtils.GetDbInt(reader["IsActive"]); entity.SystemId = StringUtils.GetDbInt(reader["SystemId"]); } } return(entity); }
/// <summary> /// 插入一条记录到表MemFavorites,如果表中存在自增字段,则返回值为新记录的自增字段值,否则返回0 /// </summary> /// <param name="db">数据库操作对象</param> /// <param name="memFavorites">待插入的实体对象</param> public MemFavoritesEntity AddMemFavorites(MemFavoritesEntity entity) { string sql = @"IF ( SELECT COUNT(1) FROM MemFavorites WHERE MemId = @MemId AND ProductDetailId = @ProductDetailId ) > 0 BEGIN UPDATE MemFavorites SET IsActive = 1 , updatetime = GETDATE() WHERE MemId = @MemId AND ProductDetailId = @ProductDetailId END ELSE BEGIN INSERT INTO MemFavorites ( [ProductDetailId] , [MemId] , [CreateTime] , UpdateTime , [IsActive] , [SystemId] ) VALUES ( @ProductDetailId , @MemId , GETDATE() , GETDATE() , @IsActive , @SystemId ) END SELECT * FROM MemFavorites WHERE MemId=@MemId AND ProductDetailId=@ProductDetailId "; DbCommand cmd = db.GetSqlStringCommand(sql); db.AddInParameter(cmd, "@ProductDetailId", DbType.Int32, entity.ProductDetailId); db.AddInParameter(cmd, "@MemId", DbType.Int32, entity.MemId); db.AddInParameter(cmd, "@IsActive", DbType.Int32, entity.IsActive); db.AddInParameter(cmd, "@SystemId", DbType.Int32, entity.SystemId); using (IDataReader reader = db.ExecuteReader(cmd)) { if (reader.Read()) { entity.Id = StringUtils.GetDbInt(reader["Id"]); entity.ProductDetailId = StringUtils.GetDbInt(reader["ProductDetailId"]); entity.MemId = StringUtils.GetDbInt(reader["MemId"]); entity.CreateTime = StringUtils.GetDbDateTime(reader["CreateTime"]); entity.UpdateTime = StringUtils.GetDbDateTime(reader["UpdateTime"]); entity.IsActive = StringUtils.GetDbInt(reader["IsActive"]); entity.SystemId = StringUtils.GetDbInt(reader["SystemId"]); } } return(entity); }
/// <summary> /// 根据主键值更新记录的全部字段(注意:该方法不会对自增字段、timestamp类型字段以及主键字段更新!如果要更新主键字段,请使用Update方法)。 /// 如果数据库有数据被更新了则返回True,否则返回False /// </summary> /// <param name="db">数据库操作对象</param> /// <param name="memFavorites">待更新的实体对象</param> public int UpdateMemFavorites(MemFavoritesEntity entity) { string sql = @" UPDATE dbo.[MemFavorites] SET [ProductDetailId]=@ProductDetailId,[MemId]=@MemId,[CreateTime]=@CreateTime,[IsActive]=@IsActive,[SystemId]=@SystemId WHERE [Id]=@id"; DbCommand cmd = db.GetSqlStringCommand(sql); db.AddInParameter(cmd, "@Id", DbType.Int32, entity.Id); db.AddInParameter(cmd, "@ProductDetailId", DbType.Int32, entity.ProductDetailId); db.AddInParameter(cmd, "@MemId", DbType.Int32, entity.MemId); db.AddInParameter(cmd, "@CreateTime", DbType.DateTime, entity.CreateTime); db.AddInParameter(cmd, "@IsActive", DbType.Int32, entity.IsActive); db.AddInParameter(cmd, "@SystemId", DbType.Int32, entity.SystemId); return(db.ExecuteNonQuery(cmd)); }
/// <summary> /// 读取记录列表。 /// </summary> /// <param name="db">数据库操作对象</param> /// <param name="columns">需要返回的列,不提供任何列名时默认将返回所有列</param> public IList <MemFavoritesEntity> GetMemFavoritesList(int pagesize, int pageindex, ref int recordCount) { string sql = @"SELECT [Id],[ProductDetailId],[MemId],[CreateTime],[IsActive],[SystemId] FROM (SELECT ROW_NUMBER() OVER (ORDER BY Id desc) AS ROWNUMBER, [Id],[ProductDetailId],[MemId],[CreateTime],[IsActive],[SystemId] from dbo.[MemFavorites] WITH(NOLOCK) WHERE 1=1 ) as temp where rownumber BETWEEN ((@PageIndex - 1) * @PageSize + 1) AND @PageIndex * @PageSize" ; string sql2 = @"Select count(1) from dbo.[MemFavorites] with (nolock) "; IList <MemFavoritesEntity> entityList = new List <MemFavoritesEntity>(); DbCommand cmd = db.GetSqlStringCommand(sql); db.AddInParameter(cmd, "@PageIndex", DbType.Int32, pageindex); db.AddInParameter(cmd, "@PageSize", DbType.Int32, pagesize); using (IDataReader reader = db.ExecuteReader(cmd)) { while (reader.Read()) { MemFavoritesEntity entity = new MemFavoritesEntity(); entity.Id = StringUtils.GetDbInt(reader["Id"]); entity.ProductDetailId = StringUtils.GetDbInt(reader["ProductDetailId"]); entity.MemId = StringUtils.GetDbInt(reader["MemId"]); entity.CreateTime = StringUtils.GetDbDateTime(reader["CreateTime"]); entity.IsActive = StringUtils.GetDbInt(reader["IsActive"]); entity.SystemId = StringUtils.GetDbInt(reader["SystemId"]); entityList.Add(entity); } } cmd = db.GetSqlStringCommand(sql2); using (IDataReader reader = db.ExecuteReader(cmd)) { if (reader.Read()) { recordCount = StringUtils.GetDbInt(reader[0]); } else { recordCount = 0; } } return(entityList); }
/// <summary> /// 读取记录列表。 /// </summary> /// <param name="db">数据库操作对象</param> /// <param name="columns">需要返回的列,不提供任何列名时默认将返回所有列</param> public IList <MemFavoritesEntity> GetMemFavoritesAll() { string sql = @"SELECT [Id],[ProductDetailId],[MemId],[CreateTime],[IsActive],[SystemId] from dbo.[MemFavorites] WITH(NOLOCK) "; IList <MemFavoritesEntity> entityList = new List <MemFavoritesEntity>(); DbCommand cmd = db.GetSqlStringCommand(sql); using (IDataReader reader = db.ExecuteReader(cmd)) { while (reader.Read()) { MemFavoritesEntity entity = new MemFavoritesEntity(); entity.Id = StringUtils.GetDbInt(reader["Id"]); entity.ProductDetailId = StringUtils.GetDbInt(reader["ProductDetailId"]); entity.MemId = StringUtils.GetDbInt(reader["MemId"]); entity.CreateTime = StringUtils.GetDbDateTime(reader["CreateTime"]); entity.IsActive = StringUtils.GetDbInt(reader["IsActive"]); entity.SystemId = StringUtils.GetDbInt(reader["SystemId"]); entityList.Add(entity); } } return(entityList); }
/// <summary> /// 更新一条MemFavorites记录。 /// 该方法提供给界面等UI层调用 /// </summary> /// <param name="memFavorites">待更新的实体对象</param> /// <param name="columns">要更新的列名,不提供任何列名时默认将更新主键之外的所有列</param> public int UpdateMemFavorites(MemFavoritesEntity memFavorites) { return(MemFavoritesDA.Instance.UpdateMemFavorites(memFavorites)); }
/// <summary> /// 插入一条记录到表MemFavorites,如果表中存在自增字段,则返回值为新记录的自增字段值,否则返回0。 /// 该方法提供给界面等UI层调用 /// </summary> /// <param name="memFavorites">要添加的MemFavorites数据实体对象</param> public MemFavoritesEntity AddMemFavorites(MemFavoritesEntity memFavorites) { return(MemFavoritesDA.Instance.AddMemFavorites(memFavorites)); }