Public Shared Function bodylist(ByVal topics As Integer, ByVal NavigationPanel As Panel)
        Dim db As DB = New DB
        Dim sSQL As String
        Dim drDB As SqlClient.SqlDataReader
        sSQL = "select * from Topic where AutoInc =" & topics
        drDB = db.GetDataReader(sSQL)
        If drDB.Read() Then
            bodylist(drDB.Item("ParentID"), NavigationPanel)
            Dim hyl As New HyperLink
            NavigationPanel.Controls.Add(New LiteralControl("<td align = right WIDTH: 20%>>>&nbsp;&nbsp;"))
            hyl.ID = drDB.Item("TopicName")
            hyl.Text = drDB.Item("TopicName")
            If IsDBNull(drDB.Item("TopicUrl")) Then
                hyl.NavigateUrl = ""
            Else
                hyl.NavigateUrl = "javascript:hrefLocation('" & drDB.Item("TopicUrl") & "')"
            End If            hyl.ToolTip = IIf(IsDBNull(drDB.Item("TopicTitle")), "", drDB.Item("TopicTitle"))
            NavigationPanel.Controls.Add(hyl)
            NavigationPanel.Controls.Add(New LiteralControl("<td>"))
            drDB.Close()
            db.Dispose()
        End If
        drDB.Close()
        db.Dispose()
    End Function
db为数据库连接类
drDB为读数据库的方法。我这段代码用了递归,然后出现了个最大连接池的问题。
原因之所以在
是因为我在没有释放Connection ,而进行了递归,当中又连接Connection。
我尝试过多种方法,未果。
最后我尝试在后面用了db.dispose()释放了,但为什么还会不定时的报错。
有时候是不报错的。是不是.Net里就不允许用递归操作。
请问有什么办法能够解决吗?谢谢了。

解决方案 »

  1.   

    你是想做一个“树”吧可以先把数据放到变量里 ,然后关掉connect,然后在调用递归,在给hyl的属性 负值 另外 Dim db As  New DB 这么写就可以了吧
      

  2.   

    我尝试过用动态变量来把第一次递归取到的值先付值,然后,等到后面要用的时候,再取回来。但是好像没有想像中的简单,不象ASP可以动态的付变量
    eg
    dim aa as string
    dim i as  integer
    aa&i =drDB.item("xxx")
    这样的话,aa&i会报错。
    所以这种方案也搁浅了。但是效果的本身无法回避递归,只能用递归去实现他所以....
    难道我还要改写到ASP?然后通过ASP在调用回来?
      

  3.   

    谢谢。jyk(jyk) 
    我是想做个树。但你说先放到变量里去,问题是我这些变量也是动态的啊。我该怎么去付值呢?
    就是说我怎么去取道这些变量的名称呢?
      

  4.   

    Public Shared Function bodylist(ByVal topics As Integer, ByVal NavigationPanel As Panel)
            Dim db As DB = New DB
            Dim sSQL As String
            Dim drDB As SqlClient.SqlDataReader
            sSQL = "select * from Topic where AutoInc =" & topics
            drDB = db.GetDataReader(sSQL)
            If drDB.Read() Then
    '''''''''''''''''''''''''''''''''''''
    '注意这里
    '''''''''''''''''''
       dim myParentID = drDB.Item("ParentID")  as string
       dim myTopicName = drDB.Item("TopicName")
       dim myTopicUrl = drDB.Item("TopicUrl")
    '在这里关掉连接 drDB.Close()
                db.Dispose()......''''''''''''''''''''''''''
    '然后递归
                bodylist(myParentID , NavigationPanel)
                Dim hyl As New HyperLink
                NavigationPanel.Controls.Add(New LiteralControl("<td align = right WIDTH: 20%>>>&nbsp;&nbsp;"))'''''''''''''''''''''
    '在这里取变量 负值
     ''''''''''''''''''''''''
                hyl.ID = myTopicName  '还有这里            hyl.Text = drDB.Item("TopicName")
                If IsDBNull(drDB.Item("TopicUrl")) Then
                    hyl.NavigateUrl = ""
                Else
                    hyl.NavigateUrl = "javascript:hrefLocation('" & drDB.Item("TopicUrl") & "')"
                End If            hyl.ToolTip = IIf(IsDBNull(drDB.Item("TopicTitle")), "", drDB.Item("TopicTitle"))
                NavigationPanel.Controls.Add(hyl)
                NavigationPanel.Controls.Add(New LiteralControl("<td>"))
                       End If
            drDB.Close()
            db.Dispose()
        End Function
      

  5.   

    那你就不要用DataReader了
    用DataSet吧
      

  6.   

    顺便问一下,
    用dataSet()
    我递归了以后,会不会递归把dataSet()在内存中的虚拟表给覆盖掉啊。因为我深怕dataSet()经过递归了后把DataTable给覆盖掉,这样到后来我就白干了。再说如果成功的话,dataSet()递归,内存中一下子多了那么多表,不知道各算不各算。
    挨,又是个问题。用cache?
    我脑子也混了。
      

  7.   

    用dataset 比较好,先取数据集,再递归
      

  8.   

    用DataView 存储数里的所有节点,然后用rowfiled (就是过虑了,好像是这么写)取得每个节点的内容。
      

  9.   


    把数据都先放到DataSet中,然后,然后递归时,每次用DataView在DataSet中查询需要的记录处理
      

  10.   

    leo2003说的方法不错的.具体的你去微软下载一个树控伯皇例子就行了.例子里很清楚的.
      

  11.   

    饿,谢谢各位,不知道效率如何。
    递归如果多的话,就完了。
    内存要死掉了。
    这个是我用dataSet()最担心的问题。