开发者

Data Reader formatting output

I'm using the following function to generate a list of users connected to a selected database. How would I change this to a single line for multiple identical results? For example: "sa (3) - MYCOMPUTER" rather than listing "sa - MYCOMPUTER" three times?

Function ConnectedUsers(ByVal SelectedDatabase As String, ByVal SelectedInstance As String)
    Dim myCommand As SqlCommand
    Dim dr As SqlDataReader
    Dim mystring As String = String.Empty
    Try
        Dim myConn As New SqlConnection(ConnectionString)
        myConn.Open()
        myCommand = New SqlCommand("select loginame,hostname from sysprocesses where db_name(dbid) = '" & SelectedDatabase & ";", myConn)
        dr = myCommand.ExecuteReader()
        While dr.Read()
  开发者_开发知识库                      mystring += GetFullName(dr(0).ToString().Trim()) & " - " & dr(1).Trim() & vbCrLf
        End While
        dr.Close()
        myConn.Close()
    Catch e As Exception
        MessageBox.Show(e.Message)
    End Try
    Return mystring
End Function

Thanks.


The SQL Command should be

select loginame, count(*) as Nbr, hostname from sysprocesses where db_name(dbid) = '" & SelectedDatabase & "' group by loginame;"

and you should change the display to show the count (Nbr in this example) to be something like:

mystring += GetFullName(dr(0).ToString().Trim()) & "(" & dr(1).Trim() & ") - " & dr(2).Trim() & vbCrLf
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜