C# - the method to get a number of open Sql connections

Posted on 2011-09-05
Last Modified: 2013-12-17
What would be the best way to create a method which returns the number of open Sql connections?  Let say I'm debugging my code (or even at some points in the code) - i would like to check at some point and make sure that all connections where closed.

SqlConnection connection = new SqlConnection( connectionString)

Question by:niceguy971
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 16

Accepted Solution

Easwaran Paramasivam earned 167 total points
ID: 36486406
Effectively  create your connection object with using statement. While leaving the using block the connection will be closed automatically.

No need to verify whether the connection is open or not. using is your best friend.

Author Comment

ID: 36486446
It's a good approach..but it does NOT answer my original question. Let say... I would like to have a method which returns the number of open Sql connections. How to create such method? It's also very interesting from the theoretical standpoint.


Assisted Solution

jagrut_patel earned 166 total points
ID: 36486463
There is no built-in support for this that I am aware of. You can try this way.

public class MyDbConnection
    private static int connectionCount;

    public MyDbConnection(string connectionString)
        this.DbConnection = new SqlConnection(connectionString);
        this.DbConnection.StateChange += new StateChangeEventHandler(connection_StateChange);

    public SqlConnection DbConnection
        private set;

    public static int ConnectionCount
            return connectionCount;

    void connection_StateChange(object sender, StateChangeEventArgs e)
        if (e.CurrentState == ConnectionState.Open)
        else if (e.CurrentState == ConnectionState.Closed)

Open in new window


string connectionString = "Your connection string";

MyDbConnection conn1 = new MyDbConnection(connectionString);

MyDbConnection conn2 = new MyDbConnection(connectionString);
using (conn2.DbConnection)

MyDbConnection conn3 = new MyDbConnection(connectionString);

MyDbConnection conn4 = new MyDbConnection(connectionString);


Open in new window

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 16

Expert Comment

by:Easwaran Paramasivam
ID: 36486477
Really intresting!! But using the 'using' keyword requires no need to verify this extra work. That is the best practice. No connection or memory leak will happen. We have used in our real project and found good improvement in performance as well.
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 167 total points
ID: 36492610
I totally agree with EaswaranP: if you're following best practices, you shouldn't need to count the number of connections  ; )

To answer your specific question, you might try this, though I can't say I've used it myself:

Author Closing Comment

ID: 36532936

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question