473,387 Members | 1,326 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Drop SQL Database

Hi,

I'm trying to:
1) create an SQL database in C#
2) open a connection to it
3) close the connection
4) drop the database

If I don't do parts 2) and 3) it works, I can create and drop a
database. If I do parts 2) and 3) the drop fails as the database is in
use. Why? Can anyone help?

Here's my test:

[Test]
public void DeleteTest()
{
SqlCommand cmd;
string sql;

string databaseName = "TEST";

SqlConnection sqlConnection = new SqlConnection(
"Server=Server1;Integrated Security=True;User
Instance=False");

sqlConnection.Open();
sql = "CREATE DATABASE " + databaseName;
cmd = new SqlCommand(sql, sqlConnection);
cmd.ExecuteNonQuery();

SqlConnection databaseConnection = new SqlConnection(
"Server=Server1;Initial Catalog=" + databaseName +
";Integrated Security=True;User Instance=False");
databaseConnection.Open();
databaseConnection.Close();

System.Threading.Thread.Sleep(10000);
sql = "DROP DATABASE " + databaseName;
cmd = new SqlCommand(sql, sqlConnection);
cmd.ExecuteNonQuery();
}

Any help would be greatly appriciated.

Thanks,

Bob

Aug 16 '06 #1
2 15835
I think connection pooling is preventing it to happen. Once a connection is
opened, ADO.Net has pooled it. And when you tried to drop the table, it saw
there is an open connection.
Try turning off connection pooling. You can add one more entry in connection
string to try it.

Pooling=false;

<bo**********@hotmail.comwrote in message
news:11**********************@h48g2000cwc.googlegr oups.com...
Hi,

I'm trying to:
1) create an SQL database in C#
2) open a connection to it
3) close the connection
4) drop the database

If I don't do parts 2) and 3) it works, I can create and drop a
database. If I do parts 2) and 3) the drop fails as the database is in
use. Why? Can anyone help?

Here's my test:

[Test]
public void DeleteTest()
{
SqlCommand cmd;
string sql;

string databaseName = "TEST";

SqlConnection sqlConnection = new SqlConnection(
"Server=Server1;Integrated Security=True;User
Instance=False");

sqlConnection.Open();
sql = "CREATE DATABASE " + databaseName;
cmd = new SqlCommand(sql, sqlConnection);
cmd.ExecuteNonQuery();

SqlConnection databaseConnection = new SqlConnection(
"Server=Server1;Initial Catalog=" + databaseName +
";Integrated Security=True;User Instance=False");
databaseConnection.Open();
databaseConnection.Close();

System.Threading.Thread.Sleep(10000);
sql = "DROP DATABASE " + databaseName;
cmd = new SqlCommand(sql, sqlConnection);
cmd.ExecuteNonQuery();
}

Any help would be greatly appriciated.

Thanks,

Bob

Aug 16 '06 #2
That's because ADO.NET keeps connections open internally for connection
pooling.

If you are using .NET 2.0, call SqlConnection.ClearPool() or ClearAllPools()
after closing the connection so that all pooled connections are closed.
Alternatively, set Pooling=false in the connection string when opening the
connection.

<bo**********@hotmail.comwrote in message
news:11**********************@h48g2000cwc.googlegr oups.com...
Hi,

I'm trying to:
1) create an SQL database in C#
2) open a connection to it
3) close the connection
4) drop the database

If I don't do parts 2) and 3) it works, I can create and drop a
database. If I do parts 2) and 3) the drop fails as the database is in
use. Why? Can anyone help?

Here's my test:

[Test]
public void DeleteTest()
{
SqlCommand cmd;
string sql;

string databaseName = "TEST";

SqlConnection sqlConnection = new SqlConnection(
"Server=Server1;Integrated Security=True;User
Instance=False");

sqlConnection.Open();
sql = "CREATE DATABASE " + databaseName;
cmd = new SqlCommand(sql, sqlConnection);
cmd.ExecuteNonQuery();

SqlConnection databaseConnection = new SqlConnection(
"Server=Server1;Initial Catalog=" + databaseName +
";Integrated Security=True;User Instance=False");
databaseConnection.Open();
databaseConnection.Close();

System.Threading.Thread.Sleep(10000);
sql = "DROP DATABASE " + databaseName;
cmd = new SqlCommand(sql, sqlConnection);
cmd.ExecuteNonQuery();
}

Any help would be greatly appriciated.

Thanks,

Bob

Aug 16 '06 #3

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

Similar topics

0
by: Jim Jawn | last post by:
I have done the beyond foolish... I accidentally clicked the DROP button on phpadmin for the horde database that runs our schools email server. I am in a world of hurt if I can't undo this...
1
by: MailSurfer | last post by:
Hi all, OS. linux 2.4.22 MYsql : 4.0.18-standard I created a database and populate 2 big InnoDB tables, the total size is > 3G. After all testing, I droped the database . But, the...
3
by: M A Srinivas | last post by:
Hello, I would like to know is it possible to disable drop database for sa or sysadmin. If saor sysadmin needs to drop the database , he/she may have to change status in one of the system...
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: aixunix | last post by:
Hi Experts, I am working on DB2 UDB on AIX and I get following OSERR message in db2diag.log when I drop database. Well, the database can be dropped successfully, but the error message is...
7
by: Risen | last post by:
Hi,all, I want to execute SQL command " DROP DATABASE mydb" and "Restore DATABASE ....." in vb.net 2003. But it always shows error. If any body can tell me how to execute sql command as above?...
5
by: Robert Fitzpatrick | last post by:
What does it mean when you drop a database and then recreate with the same name and all the objects are still there. I want to wipe out the db and put back from pg_restore. After I re-create the...
0
by: Jeff Beem | last post by:
In ASP.net 2.0 (it may be possible in previous versions also, I don't know) you can drag a database table from the server explorer to the webform and it will simply run. At least, that's what...
4
by: Michael C | last post by:
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...
5
by: pedalpete | last post by:
I think I somehow REALLY screwed up my database. I get no return making queries, REPAIR said it was all ok, I mysqldump was hanging, etc. etc, lots of nasty stuff. So, I decided to drop the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
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...

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.