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
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C# Comparing String Arrays. 5 39
EF5: Question about Metadata Artifact Processing 4 15
Store results in 3 20
Convert datetime to time string 10 19
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

816 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now