Friday, March 9, 2012

How to drop a database in SQL Server 2005 when there are some connections on the database

When there is a conections on a database,then we can't drop the database.But SQL Server 2005 can do it,because SQL Server can close existing conections before drop the database.
I wonder how can i do it too using class SQLCommand and SQLConnection?

Hi
you may run the internal stored procedure SP_Who or SP_Who2 which will retrives all the connections on the server then you may filter the results on your database which you want to drop then using the Kill SPID command you can close the connection to the database
then drop it.|||

HI Eisa:
I do it according to what you said, but i can't close the
connection to the database.

|||

msra_ken, Eisa,

perhaps i had a similar problem. I was generating a test database using a connection string to the master database. I would use a second connection string to the test database to perform some table creations, etc. Then I would try to use the master connection string to then drop the test database, but the test database would say it had connections and it couldn't be dropped.

what solved this problem was setting "Pooling=False" in the connection string to the test database. So, apparently the connection was kept alive in the pool. There are, of course, time/resource allocation draw backs to not using pooling, but in non-performance intensive situations it doesn't make a difference.
Best Wishes,

Rana Ian

|||sands_of_time:
Perhaps the solution you provided may be valid at some time, but often
some other people connect to the database which we want to drop, so,
we can't control other person how to use the connection string. On the other
hand,perhaps other people open the database which we want to drop in the
SQL Server envrionment directly.
SQL Server 2005 can solve this problem,but i don't know how it does so.

No comments:

Post a Comment