Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Get Number of Active Connections to Database in SQL Server

Dec 24, 2012
Introduction

Here I will explain how to get or find number of active connections to database in SQL Server

Description

In previous articles I explained Read XML file data using SQL Server, Joins in SQL Server, Get Latest Unique Records by count in SQL Server, Insert data with apostrophe in SQL Server, Convert Rows to Columns in SQL and many articles relating to SQL Server.  Now I will explain how to get or find number of active connections to database in  SQL Server.

To get all information related SQL database connections we can use below queries


EXEC SP_WHO

OR

EXEC SP_WHO2

Above queries will return information about current users, sessions, and processes in an instance of the Microsoft SQL Server Database Engine. The information can be filtered to return only those processes that are not idle, that belong to a specific user, or that belong to a specific session.

If we run above queries we will get output like as shown below

Output

If we want to see only active connections for specific database we need to write the query like as shown below


SELECT SPID,STATUS,PROGRAM_NAME,LOGINAME,HOSTNAME,CMD
FROM MASTER.DBO.SYSPROCESSES WHERE DB_NAME(DBID) = 'your database name' AND DBID != 0
In above query you need to replace 'your database name' with your database name. Once we replace with database name and if we run above query we will get output like as shown below

Output


If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

1 comments :

André said...

There's an automatic way to handle that kind of process? My db always keeps the "awaiting command" cmd when some user makes the login on my C# application.

Could be treated from my app side?

Thanks again.

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.