从数据库优化树视图人口
下面是我用来将数据从数据库加载到树视图中的代码!但我觉得检索时有点滞后……有什么方法可以优化它吗?
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Try
If Not IsPostBack Then
PopulateRootLevel("0", "ALL")
End If
Catch ex As Exception
MSGMgr.errHandlerSys(ex.Message, lblMsg)
End Try
End Sub
Private Sub PopulateRootLevel(ByVal LvlCD As String, ByVal scheme As String)
Dim objConn As New SqlConnection(DBMgr.asyncADOCnnStr("SQL"))
Dim objCommand As SqlCommand
If scheme = "ALL" Then
objCommand = New SqlCommand("SELECT DISTINCT LevelCD, Level1 IsChecked, LevelCD+Level2+'|'+Level1+'|'+CASE WHEN Level3 IS NULL THEN '' ELSE Level3 END ID, Level3 Scheme, GroupNo, Descriptions Title, (SELECT DISTINCT COUNT(*) FROM i_Menu_Access WHERE GroupNo = prt.GroupNo and SubItemNo1='0' AND LevelCD='0' AND ItemNo <> '0') SubItemCnt FROM i_Menu_Access prt WHERE ItemNo = '0' AND LevelCD = '" & LvlCD & "' ORDER BY GroupNo", objConn)
Else
objCommand = New SqlCommand("SELECT DISTINCT LevelCD, Level1 IsChecked, LevelCD+Level2+'|'+Level1+'|'+CASE WHEN Level3 IS NULL THEN '' ELSE Level3 END ID, Level3 Scheme, GroupNo, Descriptions Title, (SELECT DISTINCT COUNT(*) FROM i_Menu_Access WHERE GroupNo = prt.GroupNo and SubItemNo1='0' AND LevelCD='0' AND ItemNo <> '0') SubItemCnt FROM i_Menu_Access prt WHERE ItemNo = '0' AND LevelCD = '" & LvlCD & "' AND Level3 = @Scheme ORDER BY GroupNo", objConn)
objCommand.Parameters.Add("@Scheme", SqlDbType.VarChar).Value = scheme
End If
Dim da As New SqlDataAdapter(objCommand)
Dim dt As New DataTable()
da.Fill(dt)
For Each dr As DataRow In dt.Rows
Dim child As New TreeNode()
child.Text = dr("Title").ToString().Trim()
child.Value = dr("ID").ToString().Trim()
Dim GrpNo As String = dr("GroupNo").ToString().Trim()
child.ToolTip = "Click to get Child"
child.SelectAction = TreeNodeSelectAction.SelectExpand
child.CollapseAll()
tvMenuTable.Nodes.Add(child)
PopulateSubLevel(GrpNo, LvlCD, child, scheme)
Next
End Sub
Private Sub PopulateSubLevel(ByVal parentid As Integer, ByVal LvlCD As String, ByVal parentNode As TreeNode, ByVal scheme As String)
Dim objConn As New SqlConnection(DBMgr.asyncADOCnnStr("SQL"))
Dim objCommand As SqlCommand
If scheme = "ALL" Then
objCommand = New SqlCommand("SELECT DISTINCT LevelCD, Level1 IsChecked, LevelCD+Level2+'|'+Level1+'|'+CASE WHEN Level3 IS NULL THEN '' ELSE Level3 END ID, Level3 Scheme, ItemNo, cast(groupno as varchar)+'|'+cast(itemno as varchar)+'|'+cast(subitemno1 as varchar) ID2, Descriptions Title, (SELECT DISTINCT COUNT(*) FROM i_Menu_Access WHERE GroupNo = prt.GroupNo AND ItemNo = prt.ItemNo AND SubItemNo1 <> '0' AND SubItemNo2 = '0' AND LevelCD = '0') SubItemCnt FROM i_Menu_Access prt WHERE LevelCD = @LvlCD AND GroupNo = @parentID AND ItemNo <> '0' AND SubItemNo1 = '0' ORDER BY ItemNo", objConn)
objCommand.Parameters.Add("@parentID", SqlDbType.Int).Value = parentid
objCommand.Parameters.Add("@LvlCD", SqlDbType.VarChar).Value = LvlCD
Else
objCommand = New SqlCommand("SELECT DISTINCT LevelCD, Level1 IsChecked, LevelCD+Level2+'|'+Level1+'|'+CASE WHEN Level3 IS NULL THEN '' ELSE Level3 END ID, Level3 Scheme, ItemNo, cast(groupno as varchar)+'|'+cast(itemno as varchar)+'|'+cast(subitemno1 as varchar) ID2, Descriptions Title, (SELECT DISTINCT COUNT(*) FROM i_Menu_Access WHERE GroupNo = prt.GroupNo AND ItemNo = prt.ItemNo AND SubItemNo1 <> '0' AND SubItemNo2 = '0' AND LevelCD = '0') SubItemCnt FROM i_Menu_Access prt WHERE LevelCD = @LvlCD AND GroupNo = @parentID AND ItemNo <> '0' AND SubItemNo1 = '0' AND Level3 = @Scheme ORDER BY ItemNo", objConn)
objCommand.Parameters.Add("@parentID", SqlDbType.Int).Value = parentid
objCommand.Parameters.Add("@LvlCD", SqlDbType.VarChar).Value = LvlCD
objCommand.Parameters.Add("@Scheme", SqlDbType.VarChar).Value = scheme
End If
Dim da As New SqlDataAdapter(objCommand)
Dim dt As New DataTable()
da.Fill(dt)
For Each dr As DataRow In dt.Rows
Dim child As New TreeNode()
child.Text = dr("Title").ToString().Trim()
child.Value = dr("ID").ToString().Trim()
Dim ID() As String
ID = dr("ID2").ToString().Trim().Split("|")
child.ToolTip = "Click to get Child"
child.SelectAction = TreeNodeSelectAction.SelectExpand
child.CollapseAll()
parentNode.ChildNodes.Add(child)
PopulateThirdLevel(ID(0), ID(1), LvlCD, child, scheme)
Next
End Sub
Private Sub PopulateThirdLevel(ByVal lvlOneID As Integer, ByVal lvlTwoID As Integer, ByVal LvlCD As String, ByVal parentNode As TreeNode, ByVal scheme As String)
Dim objConn As New SqlConnection(DBMgr.asyncADOCnnStr("SQL"))
Dim objCommand As SqlCommand
If scheme = "ALL" Then
objCommand = New SqlCommand("SELECT DISTINCT LevelCD, Level1 IsChecked, LevelCD+Level2+'|'+Level1+'|'+CASE WHEN Level3 IS NULL THEN '' ELSE Level3 END ID, Level3 Scheme, ItemNo, SubItemNo1, cast(groupno as varchar)+'|'+cast(itemno as varchar)+'|'+cast(subitemno1 as varchar) ID2, Descriptions Title FROM i_Menu_Access WHERE LevelCD = @LvlCD AND GroupNo = @lvlOneID AND ItemNo = @lvlTwoID AND ItemNo <> '0' AND SubItemNo1 <> '0' AND SubItemNo2 = '0' ORDER BY SubItemNo1", objConn)
objCommand.Parameters.Add("@lvlOneID", SqlDbType.Int).Value = lvlOneID
objCommand.Parameters.Add("@lvlTwoID", SqlDbType.Int).Value = lvlTwoID
objCommand.Parameters.Add("@LvlCD", SqlDbType.VarChar).Value = LvlCD
Else
objCommand = New SqlCommand("SELECT DISTINCT LevelCD, Level1 IsChecked, LevelCD+Level2+'|'+Level1+'|'+CASE WHEN Level3 IS NULL THEN '' ELSE Level3 END ID, Level3 Scheme, ItemNo, SubItemNo1, cast(groupno as varchar)+'|'+cast(itemno as varchar)+'|'+cast(subitemno1 as varchar) ID2, Descriptions Title FROM i_Menu_Access WHERE LevelCD = @LvlCD AND GroupNo = @lvlOneID AND ItemNo = @lvlTwoID AND ItemNo <> '0' AND SubItemNo1 <> '0' AND SubItemNo2 = '0' AND Level3 = @Scheme ORDER BY SubItemNo1", objConn)
objCommand.Parameters.Add("@lvlOneID", SqlDbType.Int).Value = lvlOneID
objCommand.Parameters.Add("@lvlTwoID", SqlDbType.Int).Value = lvlTwoID
objCommand.Parameters.Add("@LvlCD", SqlDbType.VarChar).Value = LvlCD
objCommand.Parameters.Add("@Scheme", SqlDbType.VarChar).Value = scheme
End If
Dim da As New SqlDataAdapter(objCommand)
Dim dt As New DataTable()
da.Fill(dt)
For Each dr As DataRow In dt.Rows
Dim child As New TreeNode()
child.Text = dr("Title").ToString().Trim()
child.Value = dr("ID").ToString().Trim()
Dim ID() As String
ID = dr("ID2").ToString().Trim().Split("|")
child.SelectAction = TreeNodeSelectAction.SelectExpand
child.CollapseAll()
parentNode.ChildNodes.Add(child)
Populate4thLevel(ID(0), ID(1), ID(2), LvlCD, child, scheme)
Next
End Sub
Private Sub Populate4thLevel(ByVal lvlOneID As Integer, ByVal lvlTwoID As Integer, ByVal lvl3rdID As Integer, ByVal LvlCD As String, ByVal parentNode As TreeNode, ByVal scheme As String)
Dim objConn As New SqlConnection(DBMgr.asyncADOCnnStr("SQL"))
Dim objCommand As SqlCommand
If scheme = "ALL" Then
objCommand = New SqlCommand("SELECT DISTINCT LevelCD, Level1 IsChecked, LevelCD+Level2+'|'+Level1+'|'+CASE WHEN Level3 IS NULL THEN '' ELSE Level3 END ID, Level3 Scheme, ItemNo, SubItemNo2, Descriptions Title FROM i_Menu_Access WHERE LevelCD = @LvlCD AND GroupNo = @lvlOneID AND ItemNo = @lvlTwoID AND SubItemNo1 = @lvl3rdID AND SubItemNo2 <> '0' ORDER BY SubItemNo2", objConn)
objCommand.Parameters.Add("@lvlOneID", SqlDbType.Int).Value = lvlOneID
objCommand.Parameters.Add("@lvlTwoID", SqlDbType.Int).Value = lvlTwoID
objCommand.Parameters.Add("@lvl3rdID", SqlDbType.Int).Value = lvl3rdID
objCommand.Parameters.Add("@LvlCD", SqlDbType.VarChar).Value = LvlCD
Else
objCommand = New SqlCommand("SELECT DISTINCT LevelCD, Level1 IsChecked, LevelCD+Level2+'|'+Level1+'|'+CASE WHEN Level3 IS NULL THEN '' ELSE Level3 END ID, Level3 Scheme, ItemNo, SubItemNo2, Descriptions Title FROM i_Menu_Access WHERE LevelCD = @LvlCD AND GroupNo = @lvlOneID AND ItemNo = @lvlTwoID AND SubItemNo1 = @lvl3rdID AND SubItemNo2 <> '0' AND Level3 = @Scheme ORDER BY SubItemNo2", objConn)
objCommand.Parameters.Add("@lvlOneID", SqlDbType.Int).Value = lvlOneID
objCommand.Parameters.Add("@lvlTwoID", SqlDbType.Int).Value = lvlTwoID
objCommand.Parameters.Add("@lvl3rdID", SqlDbType.Int).Value = lvl3rdID
objCommand.Parameters.Add("@LvlCD", SqlDbType.VarChar).Value = LvlCD
objCommand.Parameters.Add("@Scheme", SqlDbType.VarChar).Value = scheme
End If
Dim da As New SqlDataAdapter(objCommand)
Dim dt As New DataTable()
da.Fill(dt)
For Each dr As DataRow In dt.Rows
Dim child As New TreeNode()
child.Text = dr("Title").ToString().Trim()
child.Value = dr("ID").ToString().Trim()
child.SelectAction = TreeNodeSelectAction.SelectExpand
child.CollapseAll()
parentNode.ChildNodes.Add(child)
Next
End Sub
Below is the code that i used to load the data into treeview from database! But i feel like it's a bit laggy while retrieving...is thr any way to optimize it?
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Try
If Not IsPostBack Then
PopulateRootLevel("0", "ALL")
End If
Catch ex As Exception
MSGMgr.errHandlerSys(ex.Message, lblMsg)
End Try
End Sub
Private Sub PopulateRootLevel(ByVal LvlCD As String, ByVal scheme As String)
Dim objConn As New SqlConnection(DBMgr.asyncADOCnnStr("SQL"))
Dim objCommand As SqlCommand
If scheme = "ALL" Then
objCommand = New SqlCommand("SELECT DISTINCT LevelCD, Level1 IsChecked, LevelCD+Level2+'|'+Level1+'|'+CASE WHEN Level3 IS NULL THEN '' ELSE Level3 END ID, Level3 Scheme, GroupNo, Descriptions Title, (SELECT DISTINCT COUNT(*) FROM i_Menu_Access WHERE GroupNo = prt.GroupNo and SubItemNo1='0' AND LevelCD='0' AND ItemNo <> '0') SubItemCnt FROM i_Menu_Access prt WHERE ItemNo = '0' AND LevelCD = '" & LvlCD & "' ORDER BY GroupNo", objConn)
Else
objCommand = New SqlCommand("SELECT DISTINCT LevelCD, Level1 IsChecked, LevelCD+Level2+'|'+Level1+'|'+CASE WHEN Level3 IS NULL THEN '' ELSE Level3 END ID, Level3 Scheme, GroupNo, Descriptions Title, (SELECT DISTINCT COUNT(*) FROM i_Menu_Access WHERE GroupNo = prt.GroupNo and SubItemNo1='0' AND LevelCD='0' AND ItemNo <> '0') SubItemCnt FROM i_Menu_Access prt WHERE ItemNo = '0' AND LevelCD = '" & LvlCD & "' AND Level3 = @Scheme ORDER BY GroupNo", objConn)
objCommand.Parameters.Add("@Scheme", SqlDbType.VarChar).Value = scheme
End If
Dim da As New SqlDataAdapter(objCommand)
Dim dt As New DataTable()
da.Fill(dt)
For Each dr As DataRow In dt.Rows
Dim child As New TreeNode()
child.Text = dr("Title").ToString().Trim()
child.Value = dr("ID").ToString().Trim()
Dim GrpNo As String = dr("GroupNo").ToString().Trim()
child.ToolTip = "Click to get Child"
child.SelectAction = TreeNodeSelectAction.SelectExpand
child.CollapseAll()
tvMenuTable.Nodes.Add(child)
PopulateSubLevel(GrpNo, LvlCD, child, scheme)
Next
End Sub
Private Sub PopulateSubLevel(ByVal parentid As Integer, ByVal LvlCD As String, ByVal parentNode As TreeNode, ByVal scheme As String)
Dim objConn As New SqlConnection(DBMgr.asyncADOCnnStr("SQL"))
Dim objCommand As SqlCommand
If scheme = "ALL" Then
objCommand = New SqlCommand("SELECT DISTINCT LevelCD, Level1 IsChecked, LevelCD+Level2+'|'+Level1+'|'+CASE WHEN Level3 IS NULL THEN '' ELSE Level3 END ID, Level3 Scheme, ItemNo, cast(groupno as varchar)+'|'+cast(itemno as varchar)+'|'+cast(subitemno1 as varchar) ID2, Descriptions Title, (SELECT DISTINCT COUNT(*) FROM i_Menu_Access WHERE GroupNo = prt.GroupNo AND ItemNo = prt.ItemNo AND SubItemNo1 <> '0' AND SubItemNo2 = '0' AND LevelCD = '0') SubItemCnt FROM i_Menu_Access prt WHERE LevelCD = @LvlCD AND GroupNo = @parentID AND ItemNo <> '0' AND SubItemNo1 = '0' ORDER BY ItemNo", objConn)
objCommand.Parameters.Add("@parentID", SqlDbType.Int).Value = parentid
objCommand.Parameters.Add("@LvlCD", SqlDbType.VarChar).Value = LvlCD
Else
objCommand = New SqlCommand("SELECT DISTINCT LevelCD, Level1 IsChecked, LevelCD+Level2+'|'+Level1+'|'+CASE WHEN Level3 IS NULL THEN '' ELSE Level3 END ID, Level3 Scheme, ItemNo, cast(groupno as varchar)+'|'+cast(itemno as varchar)+'|'+cast(subitemno1 as varchar) ID2, Descriptions Title, (SELECT DISTINCT COUNT(*) FROM i_Menu_Access WHERE GroupNo = prt.GroupNo AND ItemNo = prt.ItemNo AND SubItemNo1 <> '0' AND SubItemNo2 = '0' AND LevelCD = '0') SubItemCnt FROM i_Menu_Access prt WHERE LevelCD = @LvlCD AND GroupNo = @parentID AND ItemNo <> '0' AND SubItemNo1 = '0' AND Level3 = @Scheme ORDER BY ItemNo", objConn)
objCommand.Parameters.Add("@parentID", SqlDbType.Int).Value = parentid
objCommand.Parameters.Add("@LvlCD", SqlDbType.VarChar).Value = LvlCD
objCommand.Parameters.Add("@Scheme", SqlDbType.VarChar).Value = scheme
End If
Dim da As New SqlDataAdapter(objCommand)
Dim dt As New DataTable()
da.Fill(dt)
For Each dr As DataRow In dt.Rows
Dim child As New TreeNode()
child.Text = dr("Title").ToString().Trim()
child.Value = dr("ID").ToString().Trim()
Dim ID() As String
ID = dr("ID2").ToString().Trim().Split("|")
child.ToolTip = "Click to get Child"
child.SelectAction = TreeNodeSelectAction.SelectExpand
child.CollapseAll()
parentNode.ChildNodes.Add(child)
PopulateThirdLevel(ID(0), ID(1), LvlCD, child, scheme)
Next
End Sub
Private Sub PopulateThirdLevel(ByVal lvlOneID As Integer, ByVal lvlTwoID As Integer, ByVal LvlCD As String, ByVal parentNode As TreeNode, ByVal scheme As String)
Dim objConn As New SqlConnection(DBMgr.asyncADOCnnStr("SQL"))
Dim objCommand As SqlCommand
If scheme = "ALL" Then
objCommand = New SqlCommand("SELECT DISTINCT LevelCD, Level1 IsChecked, LevelCD+Level2+'|'+Level1+'|'+CASE WHEN Level3 IS NULL THEN '' ELSE Level3 END ID, Level3 Scheme, ItemNo, SubItemNo1, cast(groupno as varchar)+'|'+cast(itemno as varchar)+'|'+cast(subitemno1 as varchar) ID2, Descriptions Title FROM i_Menu_Access WHERE LevelCD = @LvlCD AND GroupNo = @lvlOneID AND ItemNo = @lvlTwoID AND ItemNo <> '0' AND SubItemNo1 <> '0' AND SubItemNo2 = '0' ORDER BY SubItemNo1", objConn)
objCommand.Parameters.Add("@lvlOneID", SqlDbType.Int).Value = lvlOneID
objCommand.Parameters.Add("@lvlTwoID", SqlDbType.Int).Value = lvlTwoID
objCommand.Parameters.Add("@LvlCD", SqlDbType.VarChar).Value = LvlCD
Else
objCommand = New SqlCommand("SELECT DISTINCT LevelCD, Level1 IsChecked, LevelCD+Level2+'|'+Level1+'|'+CASE WHEN Level3 IS NULL THEN '' ELSE Level3 END ID, Level3 Scheme, ItemNo, SubItemNo1, cast(groupno as varchar)+'|'+cast(itemno as varchar)+'|'+cast(subitemno1 as varchar) ID2, Descriptions Title FROM i_Menu_Access WHERE LevelCD = @LvlCD AND GroupNo = @lvlOneID AND ItemNo = @lvlTwoID AND ItemNo <> '0' AND SubItemNo1 <> '0' AND SubItemNo2 = '0' AND Level3 = @Scheme ORDER BY SubItemNo1", objConn)
objCommand.Parameters.Add("@lvlOneID", SqlDbType.Int).Value = lvlOneID
objCommand.Parameters.Add("@lvlTwoID", SqlDbType.Int).Value = lvlTwoID
objCommand.Parameters.Add("@LvlCD", SqlDbType.VarChar).Value = LvlCD
objCommand.Parameters.Add("@Scheme", SqlDbType.VarChar).Value = scheme
End If
Dim da As New SqlDataAdapter(objCommand)
Dim dt As New DataTable()
da.Fill(dt)
For Each dr As DataRow In dt.Rows
Dim child As New TreeNode()
child.Text = dr("Title").ToString().Trim()
child.Value = dr("ID").ToString().Trim()
Dim ID() As String
ID = dr("ID2").ToString().Trim().Split("|")
child.SelectAction = TreeNodeSelectAction.SelectExpand
child.CollapseAll()
parentNode.ChildNodes.Add(child)
Populate4thLevel(ID(0), ID(1), ID(2), LvlCD, child, scheme)
Next
End Sub
Private Sub Populate4thLevel(ByVal lvlOneID As Integer, ByVal lvlTwoID As Integer, ByVal lvl3rdID As Integer, ByVal LvlCD As String, ByVal parentNode As TreeNode, ByVal scheme As String)
Dim objConn As New SqlConnection(DBMgr.asyncADOCnnStr("SQL"))
Dim objCommand As SqlCommand
If scheme = "ALL" Then
objCommand = New SqlCommand("SELECT DISTINCT LevelCD, Level1 IsChecked, LevelCD+Level2+'|'+Level1+'|'+CASE WHEN Level3 IS NULL THEN '' ELSE Level3 END ID, Level3 Scheme, ItemNo, SubItemNo2, Descriptions Title FROM i_Menu_Access WHERE LevelCD = @LvlCD AND GroupNo = @lvlOneID AND ItemNo = @lvlTwoID AND SubItemNo1 = @lvl3rdID AND SubItemNo2 <> '0' ORDER BY SubItemNo2", objConn)
objCommand.Parameters.Add("@lvlOneID", SqlDbType.Int).Value = lvlOneID
objCommand.Parameters.Add("@lvlTwoID", SqlDbType.Int).Value = lvlTwoID
objCommand.Parameters.Add("@lvl3rdID", SqlDbType.Int).Value = lvl3rdID
objCommand.Parameters.Add("@LvlCD", SqlDbType.VarChar).Value = LvlCD
Else
objCommand = New SqlCommand("SELECT DISTINCT LevelCD, Level1 IsChecked, LevelCD+Level2+'|'+Level1+'|'+CASE WHEN Level3 IS NULL THEN '' ELSE Level3 END ID, Level3 Scheme, ItemNo, SubItemNo2, Descriptions Title FROM i_Menu_Access WHERE LevelCD = @LvlCD AND GroupNo = @lvlOneID AND ItemNo = @lvlTwoID AND SubItemNo1 = @lvl3rdID AND SubItemNo2 <> '0' AND Level3 = @Scheme ORDER BY SubItemNo2", objConn)
objCommand.Parameters.Add("@lvlOneID", SqlDbType.Int).Value = lvlOneID
objCommand.Parameters.Add("@lvlTwoID", SqlDbType.Int).Value = lvlTwoID
objCommand.Parameters.Add("@lvl3rdID", SqlDbType.Int).Value = lvl3rdID
objCommand.Parameters.Add("@LvlCD", SqlDbType.VarChar).Value = LvlCD
objCommand.Parameters.Add("@Scheme", SqlDbType.VarChar).Value = scheme
End If
Dim da As New SqlDataAdapter(objCommand)
Dim dt As New DataTable()
da.Fill(dt)
For Each dr As DataRow In dt.Rows
Dim child As New TreeNode()
child.Text = dr("Title").ToString().Trim()
child.Value = dr("ID").ToString().Trim()
child.SelectAction = TreeNodeSelectAction.SelectExpand
child.CollapseAll()
parentNode.ChildNodes.Add(child)
Next
End Sub
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,如果您正确设计数据库,那么您将只需要一个函数来获取和获取数据。填充任意级别的节点。例如,假设有一个示例数据库表“Items”,例如
请注意,我们已将 FK 放在具有相同
Items
表的ParentId
上,并且可以为 NULL 表示根级别项目。现在,您可以使用查询获取任何级别的项目,例如当您想要检索根级别项目时,将
@ParentId
传递为 NULL - 获取展开任何节点并获取子节点,传递ItemId< /code> 将该节点作为上面查询的
ParentId
。Well, if you design your data-base correctly then you will need only one function to get & fill nodes at any level. For example, let's say consider a sample database table 'Items' such as
Note that we have put FK on
ParentId
with the sameItems
table and that can be NULL indicating root level items. Now, you can get items at any level using query such asWhen you want to retrieve root level items, pass
@ParentId
as NULL - get expand any node and get childs, pass theItemId
for that node asParentId
to above query.您可以将数据表添加到数据集中,并在数据集中设置父子关系。
这是完整、干净且优化的代码。我在必要的地方写了评论
You can add your datatables in a Dataset and setup the parent and child relationship in the Dataset.
Here is the complete, clean and optimized code. I wrote comments where necessary