473,473 Members | 1,870 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Cannot drop database

Hi All,

I'm trying to drop an sqlserver database from c# but can't because it is
claiming it is in use. As I don't have a connection to it it must be
connection pooling that is causing the problem. I can test this by disabling
connection pooling and the database can be dropped. Is there any way to
clear out the connection pool? I can't disable connection pooling as my app
uses it. Some sample code is below

Thanks
Michael
private void DoIt()
{
Execute("CREATE DATABASE ABC");
//remove this next line and it works
Execute("SELECT * FROM sysobjects", "ABC", false);
Execute("SELECT * FROM sysobjects", "ABC", true);
Execute("DROP DATABASE ABC");
}

private void Execute(string SQL)
{
Execute(SQL, "", false);
}

private void Execute(string SQL, string Database, bool NoPooling)
{
string connect = "Data Source=(local);Integrated
Security=SSPI;";
if (Database.Length 0) connect += "Initial Catalog=" +
Database + ";";
if (NoPooling) connect += "Pooling=False;";
SqlConnection connection = new SqlConnection(connect);
connection.Open();
SqlCommand command = new SqlCommand(SQL, connection);
command.ExecuteNonQuery();
command.Dispose();
connection.Close();
}
Sep 5 '07 #1
4 2880
Michael C wrote:
I'm trying to drop an sqlserver database from c# but can't because it is
claiming it is in use. As I don't have a connection to it it must be
connection pooling that is causing the problem. I can test this by disabling
connection pooling and the database can be dropped. Is there any way to
clear out the connection pool? I can't disable connection pooling as my app
uses it.
A dirty workaround is SP_WHO and KILL !

Arne
Sep 5 '07 #2
Have you tried SqlConnection.ClearPool(connection) ?

------------------------
Regards,
Steve
www.foxville.ch

On Sep 4, 10:12 pm, "Michael C" <m...@nospam.comwrote:
Hi All,

I'm trying to drop an sqlserver database from c# but can't because it is
claiming it is in use. As I don't have a connection to it it must be
connection pooling that is causing the problem. I can test this by disabling
connection pooling and the database can be dropped. Is there any way to
clear out the connection pool? I can't disable connection pooling as my app
uses it. Some sample code is below

Thanks
Michael

private void DoIt()
{
Execute("CREATE DATABASE ABC");
//remove this next line and it works
Execute("SELECT * FROM sysobjects", "ABC", false);
Execute("SELECT * FROM sysobjects", "ABC", true);
Execute("DROP DATABASE ABC");
}

private void Execute(string SQL)
{
Execute(SQL, "", false);
}

private void Execute(string SQL, string Database, bool NoPooling)
{
string connect = "Data Source=(local);Integrated
Security=SSPI;";
if (Database.Length 0) connect += "Initial Catalog=" +
Database + ";";
if (NoPooling) connect += "Pooling=False;";
SqlConnection connection = new SqlConnection(connect);
connection.Open();
SqlCommand command = new SqlCommand(SQL, connection);
command.ExecuteNonQuery();
command.Dispose();
connection.Close();
}

Sep 5 '07 #3
Michael C (mi**@nospam.com) writes:
I'm trying to drop an sqlserver database from c# but can't because it is
claiming it is in use. As I don't have a connection to it it must be
connection pooling that is causing the problem. I can test this by
disabling connection pooling and the database can be dropped. Is there
any way to clear out the connection pool? I can't disable connection
pooling as my app uses it. Some sample code is below
Before you drop the database, run

USE db
ALTER DATABASE db SET SINGLE_USER WITH ROLLBACK IMMEDIATE
USE master
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Sep 5 '07 #4
<wi*****@googlemail.comwrote in message
news:11*********************@57g2000hsv.googlegrou ps.com...
Have you tried SqlConnection.ClearPool(connection) ?
Yikes! Sorry for the dumb question, I was assuming it wasn't going to be so
straight forward! I went straight to google but couldn't find the right
terms. I probably wouldn't have thought to look for static methods tho :-)

Thanks Arne and Erland also useful information.

Michael
Sep 5 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: BashiraInTrouble | last post by:
Hi Friends, I have tried almost everything but I cant seem to shrink the transaction log. Executing DBCC SQLPERF(LOGSPACE) gives me this info: Database Log Size (MB) Log Space Used (%) ...
2
by: Ralph Smith | last post by:
I'm having trouble copying a database to another machine. Here are the two table's in ths database and the sql commands: DROP TABLE IF EXISTS `clients`; CREATE TABLE `clients` ( `client_id`...
3
by: tbone | last post by:
We were using sql authentication on our sql server 2000 servers, But need to use windows authentication. I have an sql user jdoe that owns many different objects in multiple databases. I have...
1
by: tgru | last post by:
I am trying to drop a database, but keep getting the following error. "cannot drop database 'blah' because it is currently being used for replication". This db is not currently being...
7
by: Wayne Brantley | last post by:
I have found what appears to be an error in streaming with Datasets. It causes an error of 'Cannot find relation 0' when recreating the dataset from a stream. Here is how you reproduce it. ...
6
by: Lee Harr | last post by:
I have a database where I remove the schema public. When I try to use the createlang script, it fails like this ... >createdb foo CREATE DATABASE >psql foo -c "select version()" version...
1
by: bennett | last post by:
I'm trying to write an ASPX page that uses an OleDbDataAdapter to get data from a SQL Server database. When I test the connection, it returns success, and when I preview the data using the...
2
by: leeperman | last post by:
In Dreaweaver I cannot filter my database results to display only specific data that is retrieved from mulptile drop down list on my search page. The drop down list selections are posted to my...
3
by: Rahul B | last post by:
Hi, I have a user UCLDEV1 which is a part of staff and a group(db2schemagrp1) to which i have not given any permissions. The authorizations of that user are shown as db2 =get authorizations...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
1
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.