Jeff Moretti
Jeff Moretti

Reputation: 683

C# - SMO - How can I check to see if I have exclusive access to a given DB (ie check to see if a database is in use)?

I just spent a long time trying to figure this one out, so Id thought Id post my solution.

QUESTION: "I am about to restore a given database in C# using SMO, and would like to check to see if I have access to the db before I restore. How do I do that?"

Upvotes: 2

Views: 79

Answers (1)

Jeff Moretti
Jeff Moretti

Reputation: 683

ANSWER:

Use the following code to see if a given database is in use:

Server srv = new Server();
if (srv.GetActiveDBConnectionCount(dbName) > 0)
{
    MessageBox.Show("Database '" + dbName + "' is currently in use");
}

Upvotes: 1

Related Questions