473,396 Members | 2,154 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Checking SQL Server for active database connections in C#?

I'm writing a program that connects to MS SQL Server, on start up it clears some tables in one database, and repopulates them from another database. I would like this to only happen if there are no other connections to the database I'm clearing. Is there any way of checking the amount of active connections on a specfic database within code?

Checking active connections by user is also an option if thats the only way to do it as all connections to the database should be a specific user. If it's an admin I'm assuming it is via SQL Server Manager and want everything to behave as if they weren't connected. Google hasn't provided me with any answers as of yet, which is probably my fault, but i'm giving myself the benefit of the doubt and blaming Google. So I'll keep looking.

Thanks.
Mar 10 '08 #1
3 2684
kenobewan
4,871 Expert 4TB
I believe that you may run a stored procedure that uses exec sp_who, to give you a count of how many active connections. HTH.
Mar 10 '08 #2
thanks for the reply, it's lead me to the solution.

In the sp_who procedure it selects from the "sysprocesses" table in the master database. I took that query a modified it slightly so that it tailored to the database I wanted, and now whenever there is an open connection this select statement will tell me how many, which user and on what PC they are connected:

select spid, ecid, status, loginame=rtrim(loginame), hostname, blk=convert(char(5),blocked),
dbname = case
when dbid = 0 then null
when dbid <> 0 then db_name(dbid)
end , cmd, request_id
from master.dbo.sysprocesses
where db_name(dbid) = '*Database Name*' AND dbid <> 0
Mar 10 '08 #3
Plater
7,872 Expert 4TB
I like sp_who2 better because of its extended details/columns, and it appears to be what sql management studio uses
Mar 10 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Steven Scaife | last post by:
Below is my ASP page, I have changed the update to read DRIAL which doesn't exist, shouldnt this throw an error, or if the connection cannot be made shouldnt it throw an error as well thanks in...
2
by: Mark | last post by:
Hi - I have a rather unreliable host just now - but they offer .net, sql server and SSL for a reasonable price. Problem is, the domain is hosted on a shared server - and it keeps going down...
16
by: MLH | last post by:
Using MS Access, I have attached to MySQL servers in other states and other countries on the other side of my router. But when I use the MySQL ODBC driver 3.51 to connect to a MySQL server on my...
3
by: fyi85 | last post by:
I am trying to create a server on the same machine running the same instance. This is v8.1.5 on Windows 2003 Server. The instance is set to FEDERATED=YES The server will be created for database...
4
by: Greg P | last post by:
I know this is a long post, please bear with me. I have been working on this all weekend to no avail although I have done a good amount of research (see most pertinent links that I've looked at...
7
by: Greg P | last post by:
I know this is a long post, please bear with me. I have been working on this all weekend to no avail although I have done a good amount of research (see most pertinent links that I've looked at...
2
by: Venkata Narayana | last post by:
Hi, You all may be knowing that Connection.isClosed() does not tells us if the underying DB connection is active or not; it only checks if Connection.close() had been previously called or not....
1
by: sherifbk | last post by:
Problem description ============== - I have 4 clients and 1 server (SQL server) - 3 clients are Monitoring console 1 client is operation console - Monitoring console collects some data from...
2
by: aidanhaylock | last post by:
Morning, This one is really driving me insane. I am developing a site for a client who doesn't particularly want to move their hosting away from their current provider. The current host are...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.