Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dropping SQL Server database through C#

I am using this code to delete a database through C#

Int32 result = 0;

try
{
        String Connectionstring = CCMMUtility.CreateConnectionString(false, txt_DbDataSource.Text, "master", "sa", "happytimes", 1000);

        SqlConnection con = new SqlConnection();
        con.ConnectionString = Connectionstring;

        String sqlCommandText = "DROP DATABASE [" + DbName + "]";
        if (con.State == ConnectionState.Closed)
        {
            con.Open();
            SqlConnection.ClearPool(con);
            con.ChangeDatabase("master");
            SqlCommand sqlCommand = new SqlCommand(sqlCommandText, con);
            sqlCommand.ExecuteNonQuery();
        }
        else
        {
            con.ChangeDatabase("master");
            SqlCommand sqlCommand = new SqlCommand(sqlCommandText, con);
            sqlCommand.ExecuteNonQuery();
        }



        con.Close();
        con.Dispose();
        result = 1;
    }
    catch (Exception ex)
    {
        result = 0;
    }
    return result;

But I get an error

Database currently in use

Can anyone help?

like image 778
Anshuman Jasrotia Avatar asked Dec 28 '12 12:12

Anshuman Jasrotia


People also ask

How do I completely delete a database in SQL Server?

To delete a database, connect to an instance of the SQL Server, and then expand that instance. Expand Databases, select the database which need to be deleted. Right-click the database which need to be deleted, and then click Delete.

How do I drop a database from the command line?

To do delete a database you need the command 'DROP DATABASE'. The syntax is similar to creating a database. 'DROP DATABASE <name>;', where <name> is the name of the database you want to delete.

Which database Cannot be dropped?

c# - Cannot drop database because it is currently in use - Stack Overflow. Stack Overflow for Teams – Start collaborating and sharing organizational knowledge.


3 Answers

Try this:

String sqlCommandText = @"
ALTER DATABASE " + DbName + @" SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
DROP DATABASE [" + DbName + "]";

Also make sure that your connection string defaults you to the master database, or any other database other than the one you're dropping!

As an aside, you really don't need all of that stuff around your queries. The ConnectionState will always start off Closed, so you don't need to check for that. Likewise, wrapping your connection in a using block eliminates the need to explicitly close or dispose the connection. All you really need to do is:

String Connectionstring = CCMMUtility.CreateConnectionString(false, txt_DbDataSource.Text, "master", "sa", "happytimes", 1000);

using(SqlConnection con = new SqlConnection(Connectionstring)) {
    con.Open();
    String sqlCommandText = @"
        ALTER DATABASE " + DbName + @" SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
        DROP DATABASE [" + DbName + "]";
    SqlCommand sqlCommand = new SqlCommand(sqlCommandText, con);
    sqlCommand.ExecuteNonQuery();
}
result = 1;
like image 159
Dave Markle Avatar answered Sep 23 '22 07:09

Dave Markle


Here is how you do it using Entity Framework version 6

System.Data.Entity.Database.Delete(connectionString);
like image 32
Bassem Avatar answered Sep 20 '22 07:09

Bassem


You should take a look at SMO. These allow you to manage all aspects of SQL Server from code, including deleting of databases.

The database object has a Drop method to delete database.

like image 34
Mayank Pathak Avatar answered Sep 24 '22 07:09

Mayank Pathak