Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / September 2007

Tip: Looking for answers? Try searching our database.

Cannot drop database

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Michael C - 05 Sep 2007 03:12 GMT
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();
       }
Arne Vajhøj - 05 Sep 2007 03:30 GMT
> 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
wisccal@googlemail.com - 05 Sep 2007 07:15 GMT
Have you tried SqlConnection.ClearPool(connection) ?

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

> Hi All,
>
[quoted text clipped - 36 lines]
>             connection.Close();
>         }
Michael C - 05 Sep 2007 08:55 GMT
> 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
Erland Sommarskog - 05 Sep 2007 08:24 GMT
> 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

Signature

Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.