Detach SQL-Server Database from .NET

  • Thread starter Thread starter Axel Hecker
  • Start date Start date
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.
 
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.
 
Back
Top