Solved

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

Posted on 2011-09-05
6
286 Views
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)

Thanks
0
Comment
Question by:niceguy971
6 Comments
 
LVL 16

Accepted Solution

by:
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.

http://weblogs.asp.net/jasonsalas/archive/2005/02/08/368811.aspx

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

Author Comment

by:niceguy971
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.

Thanks
0
 
LVL 8

Assisted Solution

by:jagrut_patel
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
    {
        get;
        private set;
    }

    public static int ConnectionCount
    {
        get
        {
            return connectionCount;
        }
    }

    void connection_StateChange(object sender, StateChangeEventArgs e)
    {
        if (e.CurrentState == ConnectionState.Open)
            connectionCount++;
        else if (e.CurrentState == ConnectionState.Closed)
            connectionCount--;
    }
}

Open in new window


Usage

string connectionString = "Your connection string";

MyDbConnection conn1 = new MyDbConnection(connectionString);
conn1.DbConnection.Open();
//conn1.DbConnection.Close();

MyDbConnection conn2 = new MyDbConnection(connectionString);
using (conn2.DbConnection)
{
    conn2.DbConnection.Open();
}

MyDbConnection conn3 = new MyDbConnection(connectionString);
conn3.DbConnection.Open();
//conn3.DbConnection.Close();

MyDbConnection conn4 = new MyDbConnection(connectionString);
conn4.DbConnection.Open();
conn4.DbConnection.Close();

MessageBox.Show(MyDbConnection.ConnectionCount.ToString()); 

Open in new window


HTH!
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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.
0
 
LVL 74

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:  http://fuchangmiao.blogspot.com/2009/03/sql-server-open-connections-exist-in.html
0
 

Author Closing Comment

by:niceguy971
ID: 36532936
Thanks
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

708 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

15 Experts available now in Live!

Get 1:1 Help Now