Ниже приведен код, который я использовал для загрузки данных в дерево в базе данных! Но я чувствую, что он немного запаздывает при поиске ... Есть ли способ оптимизировать его?
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