我最近问过以下问题:System.Data.SqlClient.SqlException (0x80131904)。我仔细查看了代码,并找到了一个未正确关闭的datareader。
我仍然偶尔会在日志文件中看到此异常。有没有办法计算数据库中有多少个打开的连接/数据引导器?我尝试了以下SQL语句:
SELECT DB_NAME(dbid) AS DBName,
COUNT(dbid) AS NumberOfConnections,
loginame AS LoginName,
nt_domain AS NT_Domain,
nt_username AS NT_UserName,
hostname AS HostName
FROM sys.sysprocesses
WHERE dbid > 0
GROUP BY dbid,
hostname,
loginame,
nt_domain,
nt_username
ORDER BY NumberOfConnections DESC;
它总是返回一个连接(我在SQL Studio Manager中打开的那个连接)。为什么是这样?这是权限问题吗?
答案 0 :(得分:0)
试试这个:
SELECT login_name, [program_name], COUNT(session_id) AS [session_count]
FROM sys.dm_exec_sessions WITH (NOLOCK)
GROUP BY login_name, [program_name]
ORDER BY COUNT(session_id) DESC OPTION (RECOMPILE);