简体   繁体   English

N层父/子面包屑路径-自下而上循环

[英]N-tiered Parent/Child Breadcrumb Trail - Bottom to Top Loop

I am trying to building a breadcrumb trail based on a parent/child relationship in a SQL database table. 我正在尝试基于SQL数据库表中的父/子关系构建面包屑跟踪。 I have done these in the past, but what I need to do now is walk my way up through the hierarchy from the bottom up (backwards). 我过去曾经做过这些,但是现在我需要做的是从下至上(向后)逐步完成层次结构。

Here is what my DataTable looks like: 这是我的DataTable的样子:

DocID | DocID | ParentDocID | ParentDocID | Name 名称
7 | 7 | 0 | 0 | Images (Root) 图片(根)
24 | 24 | 7 | 7 | JPG (Level 2) JPG(第2级)
34 | 34 | 24 | 24 | Size (Level 3) 大小(3级)

So this is an N-tiered architecture. 因此,这是一个N层架构。 I always know what level I am at via a query string variable. 我总是通过查询字符串变量知道我处于哪个级别。 I need to loop from the bottom up so that a user can bookmark what level in the tier structure they are at. 我需要自下而上循环,以便用户可以标记他们所在的层结构中的哪个级别。 I am having trouble figuring this out. 我很难弄清楚这一点。 I cannot use session or viewstate. 我无法使用会话或viewstate。

Desired Results: 所需结果:

User goes to this link: Default.aspx?DocId=34 – Then they see the breadcrumb trail as such: 用户转到此链接:Default.aspx?DocId = 34 –然后,他们看到面包屑跟踪如下:
Home > Images > JPG > Size 主页 > 图片 > JPG > 尺寸

Default.aspx?DocId=24 – Then they see the breadcrumb trail as such: Default.aspx?DocId = 24 –然后他们看到面包屑跟踪如下:
Home > Images > JPG 主页 > 图片 > JPG

The “Home” element I have hard-coded. 我已经硬编码了“首页”元素。 I also need each item in the trail to link except for the level you are on. 除了您所在的级别,我还需要跟踪中的每个项目进行链接。

I would rather this be done in C# code. 我宁愿用C#代码完成。 I also am open to SQL level Stored Procedure options. 我也对SQL级别的“存储过程”选项开放。

Any help would be greatly appreciated. 任何帮助将不胜感激。 Thank-you in advance! 先感谢您!

Here is some SQL I am trying but not getting the results I need. 这是我正在尝试的一些SQL,但未获得所需的结果。 I hope this helps: 我希望这有帮助:

WITH Hierachy([DocID], [ParentDocID], [Name], [Level]
AS
(
SELECT [DocID], [ParentDocID], [Name], 0 AS [Level]
FROM [Documents] d
WHERE (d.[ParentDocID] = 7) AND ([Folder] = 1) AND ([Status] = 'A') AND ([AppGroupID] = 4)
UNION ALL
SELECT d.[DocID], d.[ParentDocID], d.[Name], dh.[Level] + 1
FROM [Documents] d
INNER JOIN Hierachy dh
--ON d.ParentDocID = dh.DocID
ON dh.[ParentDocID] = d.[DocID] -- bottom up apporach
WHERE ([Folder] = 1) AND ([Status] = 'A') AND ([AppGroupID] = 4)
)
SELECT [DocID], [ParentDocID], [Name]
FROM Hierachy
--WHERE [Level] > 0

I got it! 我知道了! Here is the SQL that works: 这是有效的SQL:

WITH Hierachy([DocID], [ParentDocID], [Name], [Level])
AS
(
SELECT [DocID], [ParentDocID], [Name], 0 AS [Level]
FROM [Documents] d
WHERE (d.[DocID] = @ParentDocID) AND ([Folder] = 1) 
AND ([Status] = 'A') AND ([AppGroupID] = @AppGroupID)
UNION ALL
SELECT d.[DocID], d.[ParentDocID], d.[Name], dh.[Level] + 1
FROM [Documents] d
INNER JOIN Hierachy dh
ON dh.[ParentDocID] = d.[DocID]
WHERE ([Folder] = 1) 
AND ([Status] = 'A') AND ([AppGroupID] = @AppGroupID)
)
SELECT [DocID], [ParentDocID], [Name]
FROM Hierachy
ORDER BY [Level] DESC
)

Check below link you will get perfact answer for your question. 检查下面的链接,您将获得有关问题的准确答案。

SQL Server: How to get all child records given a parent id in a self referencing table SQL Server:如何在自引用表中获取给定父ID的所有子记录

Sure you can. 你当然可以。 You can use CURSOR or WHILE LOOP or XML. 您可以使用CURSOR或WHILE LOOP或XML。 I have an example like you. 我有一个像你这样的例子。 Here's the WHILE LOOP with a temp Table, it selects parents of a node with a provided ID: 这是带有临时表的WHILE LOOP,它选择具有提供的ID的节点的父代:

DECLARE @ID INT = 4
DECLARE @Parent_ID INT
DECLARE @Tree TABLE
(
    ID int,
    Value NVARCHAR(10),
    Parent_ID int
)

SELECT @Parent_ID = Parent_ID FROM Tree 
WHERE ID = @ID

WHILE @Parent_ID IS NOT NULL
BEGIN
    INSERT INTO @Tree
    SELECT * FROM Tree
    WHERE ID = @Parent_ID

    SELECT @Parent_ID = Parent_ID FROM Tree
    WHERE ID = @Parent_ID
END

SELECT * FROM @Tree

This describes solving a similar problem, ie walking up a tree. 描述了解决类似的问题,即,走树。 Any inline sql, such as this is best executed within a stored procedure. 诸如此类的任何内联sql最好在存储过程中执行。 Solving it in SQL is probably the best approach, as you already have all the data there. 用SQL解决它可能是最好的方法,因为您已经拥有了所有数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM