Detach SQL-Server Database from .NET

A

Axel Hecker

Hi,

I want to detach an SQL-Server database; what I'm doing
is this:

- Define an SQLCommand object with some SQLConnection
- Change the database context to 'master' by
setting "Database='master'" in the ConnectionString
property of the SQLConnection
- Set the CommandText of the SQLCommand object to
something like
"EXEC sp_detach_db @dbname=N'NameOfDatabaseToBeDetached'"
- Open the connection
- Call the ExecuteNonQuery() method of the SQLCommand
object.

My problem is: the database to be detached will always be
reported as currently in use (error number 3703).
Obviously, although I set the database context
to 'master' explicitly, the established connection to the
database to be detached seems to remain.
How can I break the connection to this database?

Thanks for an answer! Axel.
 
D

Don

Axel said:
Hi,

I want to detach an SQL-Server database; what I'm doing
is this:

- Define an SQLCommand object with some SQLConnection
- Change the database context to 'master' by
setting "Database='master'" in the ConnectionString
property of the SQLConnection
- Set the CommandText of the SQLCommand object to
something like
"EXEC sp_detach_db @dbname=N'NameOfDatabaseToBeDetached'"
- Open the connection
- Call the ExecuteNonQuery() method of the SQLCommand
object.

My problem is: the database to be detached will always be
reported as currently in use (error number 3703).
Obviously, although I set the database context
to 'master' explicitly, the established connection to the
database to be detached seems to remain.
How can I break the connection to this database?

Thanks for an answer! Axel.

When I want to muck about with SQLServer itself I use the SQLDMO interface.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top