1

when I'm trying to delete a database using c#, method

Microsoft.SqlServer.Management.Smo.Server.KillDatabase(String database).

it fails. Error message is

System.Data.SqlClient.SqlException: User does not have permission to alter database 'My_Database_Name', the database does not exist, or the database is not in a state that allows access checks. ALTER DATABASE statement failed.".

Things I know :

1) Database exists;
2) Login name and password are correct;
3) Before it worked good, but after i added 3 additional tables to the database, it stopped working.

What could be the reason, why it fails to delete a database ?

My code is :

if (server.Databases.Contains(databaseName))
{
    server.KillDatabase(databaseName);
}
2
  • Also make sure your connection string is pointing to master database instead of the My_Database_Name. I think the biggest suspect is your permissions on this server. Commented May 25, 2015 at 10:29
  • or the database is not in a state that allows access checks. ALTER DATABASE statement failed.". this line here makes me think you are connecting to the databases itself to drop it :) Commented May 25, 2015 at 10:31

1 Answer 1

2

Try like this:

sqlCommandText = "DROP DATABASE [databaseName]";
sqlCommand = new SqlCommand(sqlCommandText , connection);
sqlCommand.ExecuteNonQuery();

Assuming that the user has the permission to drop the database.

Also you could try to set

USE master; 
ALTER DATABASE databaseName SET SINGLE_USER WITH ROLLBACK IMMEDIATE; 
DROP DATABASE databaseName;
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.