2012-03-30 83 views
1

我想寫這樣的東西,有沒有什麼辦法可以從池中得到多少連接數

在我的單元測試中我想知道有沒有連接泄漏? 所以我想之前得到的從水池conections使用計數,並得到後應該是平等的,測試是STA,所以它必須努力

所以,問題是,在這個

+1

有一個表演nce計數器,你可以在Perfmon.exe中看到它。 Category =「.NET CLR Data」,Counter =「SqlClient:Current#pooled connections」。該實例應該是您的測試運行者。使用WMI或PerformanceCounter來閱讀它。 – 2012-03-30 15:38:04

回答

0

我不稱號知道這是你在找什麼,但試試看。

SELECT COUNT(*) AS Number_Of_Connections FROM sys.dm_exec_sessions WHERE program_name='your connection str application name'

或計數基於數據庫名稱

SELECT DB_NAME(dbid) as 'DbNAme', COUNT(dbid) as 'Connections' from master.dbo.sysprocesses with (nolock) WHERE dbid > 0 GROUP BY dbid

+0

在這種情況下,OP指的是特定的.NET CLR計數器。 – 2013-01-23 18:53:19

0

你應該能夠做到像下面的示例控制檯應用程序:

static void Main(string[] args) 
    { 
     using (SqlConnection conn = new SqlConnection(@"SAMPLE_CONNECTIONSTRING")) 
     { 
      conn.Open(); 
      using (SqlCommand cmd = new SqlCommand()) 
      { 
       cmd.Connection = conn; 
       cmd.CommandText = "SELECT * FROM SAMPLE_TABLE"; 
       SqlDataReader dr = cmd.ExecuteReader(); 

       //Used to show that values update based on connections in use. 
       //dr.Dispose(); 
       //cmd.Dispose(); 
       //conn.Dispose(); 

       //create our WMI searcher 
       ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from Win32_PerfRawData_NETDataProviderforSqlServer_NETDataProviderforSqlServer"); 
       //now loop through all the item found with the query 
       foreach (ManagementObject obj in searcher.Get()) 
       { 
        try 
        { 
         Console.WriteLine("Class Win32_PerfRawData_NETDataProviderforSqlServer_NETDataProviderforSqlServer"); 
         Console.WriteLine(obj.Properties["Name"].Value); 

         Console.WriteLine("Connection Pools " + obj.Properties["NumberOfActiveConnectionPools"].Value); 
         Console.WriteLine("Pooled Connections " + obj.Properties["NumberOfPooledConnections"].Value); 
         Console.WriteLine("Active Connections " + obj.Properties["NumberOfActiveConnections"].Value); 
         Console.WriteLine("Free Connections " + obj.Properties["NumberOfFreeConnections"].Value); 
        } 
        catch (Exception ex) 
        { 
         //throw new Exception("Failed to enumerate a WMI command.", ex); 
        } 
       } 
      } 
     } 
     Console.ReadLine(); 
    } 

VB Code Sample that got me the correct Win32 class.

相關問題