表A(文章表)
ID    标题表B(评论表)
tableA_ID   标题使用datagrid显示表A列表内容的时候,需要用function统计表B中的评论数量
求效率高的代码,最好不要每次都打开关闭数据库一次~我目前的代码    Public Shared Function Data_count(ByVal str As String) As String        Dim Conn As SqlConnection
        Conn = New SqlConnection(customSetting)
        Conn.Open()
        Dim Cmd As New SqlCommand("select count(id) from 表A where tablea_id=" & str, Conn)
        Dim intrs As String
        If Cmd.ExecuteScalar() Is DBNull.Value Then
            intrs = "0"
        Else
            intrs = Cmd.ExecuteScalar()
        End If
        Conn.Close()
        Return intrs
    End Function
VB和C#代码均可,谢谢!!!1

解决方案 »

  1.   

    SQL 语句修改一下,直接join上就行了
      

  2.   

    Select a.ID,a.标题,Count(b.标题) as num From 表A a Join 表B b On a.ID = b.tablea_id Group By b.标题,a.ID,a.标题不知道这样行不行,写得烂了.
      

  3.   

    表B(评论表)
    tableA_ID 标题
    中有文章的ID吧?那么一个left join 连接。
    把所有的数据取出来放在datatable中 然后进行分割 一部分显示上面。一部分显示下面评论信息这些
      

  4.   

    在表A的datagrid中直接取,不要用function做select 表A.*, (select count(id) from 表B where 表B.tableA_ID = 表A.id) as counts
    from 表A