find number of open connection on database

前端 未结 3 1810
死守一世寂寞
死守一世寂寞 2021-01-04 22:50

My web application is in asp.net 2.0,c#2.0 and sql server 208 how can i find number of open connections on my sql server 2008 database.and is there any way to clear connecti

3条回答
  •  生来不讨喜
    2021-01-04 23:23

    You might want to read up on connection pooling: http://msdn.microsoft.com/en-us/library/8xx3tyca.aspx

    A separate connection pool is created for each distinct connect string. Further, if you are connecting via integraged security and your web site is using Basic or Windows authentication (rather than anonymous), a separate connection pool will be created for each user of the web site.

    To clear connection pools, the SqlConnection object provides the methods ClearPool() and ClearAllPool()`. However, an individual connection won't be closed and removed from the pool until it is closed or disposed.

    All the various objects involved in the execution of the sql query that implement IDisposable should be wrapped in a using statement to guaranteed proper disposal. Something along these lines:

    IEnumerable list = new List() ;
    
    using ( SqlConnection  connection = new SqlConnection( credentials ) )
    using ( SqlCommand     command    = connection.CreateCommand() )
    using ( SqlDataAdapter adapter    = new SqlDataAdapter( command ) )
    using ( DataSet        results    = new DataSet() )
    {
    
      command.CommandType = CommandType.StoredProcedure ;
      command.CommandText = @"someStoredProcedure" ;
    
      try
      {
        connection.Open() ;
        adapter.Fill( results ) ;
        connection.Close() ;
    
        list = TransformResults( results ) ;
    
      }
      catch
      {
        command.Cancel() ;
        throw
      }
    
    }
    
    return list ;
    

    You can examine what SPIDs are open in Sql Server either by executing the stored procedure sp_who (must have the appropriate admin permissions in the SQL Server). You can also use perfmon.

提交回复
热议问题