Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Determine NTUserId from Spid...

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2003-08-05 : 07:21:27
Alan writes "Is there any way of determining the NTUserName from a specific Spid? SQL Profiler manages to do this but I dont want to run this to determine the NTUserName.

I am trying to use a small VB app to present a list of SPIDs together with the corresponding NT Username.

Here is an extract of the code...

' set strings
sConnect = "Provider=MSDASQL.1;Extended Properties='DRIVER=SQL Server;SERVER=" & msServer & ";UID=" & msUser & ";PWD=" & msPwd & ";DATABASE=Policy-Data'"
sSQL = "select spid,nt_username from master..sysprocesses where nt_username <> ''"

Set datPrimaryRS = New ADODB.Recordset
' open connection
Set dfwConn = New ADODB.Connection
dfwConn.Open sConnect

' ' create a recordset using the provided collection
datPrimaryRS.CursorLocation = adUseClient
datPrimaryRS.Open sSQL, dfwConn, adOpenForwardOnly, adLockReadOnly

'Reset listview
ltvUsers.ListItems.Clear

'Populate ListView with user names
Do While Not datPrimaryRS.EOF

Set objItemX = ltvUsers.ListItems.Add()
objItemX.Text = _
datPrimaryRS.Fields("spid")
objItemX.SmallIcon = "User"

If Not IsNull(datPrimaryRS.Fields("nt_username")) Then
objItemX.SubItems(1) = _
Trim(datPrimaryRS.Fields("nt_username"))
End If
datPrimaryRS.MoveNext
Loop

Set datPrimaryRS = Nothing
LockWindowUpdate 0

It appears to work but only returns 2 users both of which are Administrator. When I run the SQL from Query Analyser I get around 25 users listed. Has anyone got any ideas as to why this does not work?

Regards,

Alan"
   

- Advertisement -