上午在一個項目上工作,這是基於Web的應用程序許多開發人員都使用連接對象,但他們沒有關閉它。asp.net c#如何知道有多少個連接對象當前打開,但沒有被網頁關閉
,所以我可以我得到網頁上的細節,有多少連接對象正在使用,但沒有關閉
正在使用一些代碼,是在這裏提到
protected void btnCheckConnection_Click(object sender, EventArgs e)
{
GetIntegratedSecurityConnectionString();
SetUpPerformanceCounters();
CreateConnections();
}
private static string GetIntegratedSecurityConnectionString()
{
string @SqlConn = "Server=190.196.19.12;UID=test1;PWD=test1;Database=test";
return @SqlConn;
}
private void CreateConnections()
{
// List the Performance counters.
WritePerformanceCounters();
// Create 4 connections and display counter information.
SqlConnection connection1 = new SqlConnection(
GetIntegratedSecurityConnectionString());
connection1.Open();
Console.WriteLine("Opened the 1st Connection:");
WritePerformanceCounters();
SqlConnection connection2 = new SqlConnection(
GetSqlConnectionStringDifferent());
connection2.Open();
Response.Write("Opened the 2nd Connection:");
WritePerformanceCounters();
SqlConnection connection3 = new SqlConnection(
GetSqlConnectionString());
connection3.Open();
Console.WriteLine("Opened the 3rd Connection:");
WritePerformanceCounters();
SqlConnection connection4 = new SqlConnection(
GetSqlConnectionString());
connection4.Open();
Console.WriteLine("Opened the 4th Connection:");
WritePerformanceCounters();
connection1.Close();
Console.WriteLine("Closed the 1st Connection:");
WritePerformanceCounters();
connection2.Close();
Console.WriteLine("Closed the 2nd Connection:");
WritePerformanceCounters();
connection3.Close();
Console.WriteLine("Closed the 3rd Connection:");
WritePerformanceCounters();
connection4.Close();
Console.WriteLine("Closed the 4th Connection:");
WritePerformanceCounters();
}
private enum ADO_Net_Performance_Counters
{
NumberOfActiveConnectionPools,
NumberOfReclaimedConnections,
HardConnectsPerSecond,
HardDisconnectsPerSecond,
NumberOfActiveConnectionPoolGroups,
NumberOfInactiveConnectionPoolGroups,
NumberOfInactiveConnectionPools,
NumberOfNonPooledConnections,
NumberOfPooledConnections,
NumberOfStasisConnections,
NumberOfFreeConnections
// The following performance counters are more expensive to track.
// Enable ConnectionPoolPerformanceCounterDetail in your config file.
// SoftConnectsPerSecond
// SoftDisconnectsPerSecond
// NumberOfActiveConnections
}
private void SetUpPerformanceCounters()
{
connection.Close();
this.PerfCounters = new PerformanceCounter[11];
string instanceName = GetInstanceName();
testInstanceName = instanceName;
Type apc = typeof(ADO_Net_Performance_Counters);
int i = 0;
foreach (string s in Enum.GetNames(apc))
{
this.PerfCounters[i] = new PerformanceCounter();
this.PerfCounters[i].CategoryName = ".NET Data Provider for SqlServer";
this.PerfCounters[i].CounterName = s;
this.PerfCounters[i].InstanceName = instanceName;
i++;
}
}
[DllImport("kernel32.dll", SetLastError = true)]
static extern int GetCurrentProcessId();
private string GetInstanceName()
{
//This works for Winforms apps.
/*
string instanceName =
System.Reflection.Assembly.GetEntryAssembly().GetName().Name;
*/
//This works for Web apps.
//string instanceName = System.Environment.UserDomainName; //System.Reflection.Assembly.GetEntryAssembly().GetName().Name;
string instanceName = System.Reflection.Assembly.GetCallingAssembly().GetName().Name;
// Must replace special characters like (,), #, /, \\
string instanceName2 =
AppDomain.CurrentDomain.FriendlyName.ToString().Replace('(', '[')
.Replace(')', ']').Replace('#', '_').Replace('/', '_').Replace('\\', '_');
// For ASP.NET applications your instanceName will be your CurrentDomain's
// FriendlyName. Replace the line above that sets the instanceName with this:
// instanceName = AppDomain.CurrentDomain.FriendlyName.ToString().Replace('(','[')
// .Replace(')',']').Replace('#','_').Replace('/','_').Replace('\\','_');
string pid = GetCurrentProcessId().ToString();
instanceName = instanceName + "[" + pid + "]";
Console.WriteLine("Instance Name: {0}", instanceName);
Console.WriteLine("---------------------------");
return instanceName;
}
private void WritePerformanceCounters()
{
foreach (PerformanceCounter p in this.PerfCounters)
{
string strName = p.CounterName + " = " + p.NextValue();
}
}
如果有趣p.NextValue( )然後面臨的錯誤附加信息:實例'Dummy_Project [9544]'在指定的類別中不存在。
只要數據已被讀取/處理並且保持打開狀態,連接應該關閉。 SqlConnection也實現了iDisposable,所以你應該總是使用using-statement來進行適當的內存清理。 – Esko