0

I have to delete/drop database from code written in C# using ado.net in desktop based application using window forms. The code is as follows,

string connectionString = Helpers.Configs.ApplicationConnectionString;
SqlConnection conn2 = new SqlConnection(connectionString);
connectionString = connectionString.Replace(conn2.Database, "master");
string query = "alter database [" + DBName + "] set single_user with rollback immediate; drop database [" + DBName + "];";
bool isTrue = false;
using (SqlConnection conn = new SqlConnection(connectionString))
{
  SqlCommand cmd = new SqlCommand(query, conn);
  cmd.CommandTimeout = 0;
  conn.Open();
  cmd.ExecuteNonQuery();
  conn.Close();
  isTrue = true;
}
return isTrue;

In above code, I am using master database to connect through and executing the query. In result, database is deleted somehow but the problem is, a messagebox/alert is showing in case of ExecuteNonQuery() which is as follows,

enter image description here

I don't want this messagebox or alert to be displayed. Because this very annoying that is displayed in application on deleting database.

mschuurmans
  • 1,088
  • 1
  • 12
  • 24
Najjaf
  • 11
  • 4
  • 1
    That's not something under *your* control. That's an alert displayed by another application entirely when it finds out it's connection has been ripped out from under it. – Damien_The_Unbeliever Nov 27 '18 at 07:31
  • Possible duplicate of [SQL Server error on update command - "A severe error occurred on the current command"](https://stackoverflow.com/questions/1175244/sql-server-error-on-update-command-a-severe-error-occurred-on-the-current-com) – Tetsuya Yamamoto Nov 27 '18 at 07:35
  • If the error is coming from another application, there is not much you can do other than [check for active connections](https://stackoverflow.com/questions/1248423/how-do-i-see-active-sql-server-connections) before sending the `alter database` command. – John Wu Nov 27 '18 at 07:36
  • @TetsuyaYamamoto I went through that thread. I don't have any index on it. I am just creating database and droping it with very simple table in it. – Najjaf Nov 27 '18 at 07:36
  • Are you trying to delete the database to which you are connected? – Steve Nov 27 '18 at 07:36
  • @JohnWu I also tried to kill all the active connection by sp_who but still finding that alert. – Najjaf Nov 27 '18 at 07:37
  • @Steve nope, I am using master in the code to actively connect to it. – Najjaf Nov 27 '18 at 07:38
  • Whose message box is that? There is no code in your example that displays a message box. Is it coming from somewhere else in your program? Some other program? You will need to figure that out in order to stop it. – John Wu Nov 27 '18 at 07:40
  • @JohnWu It is coming from database on executing "ExecuteNonQuery()" statement. No other application is in middle. – Najjaf Nov 27 '18 at 07:42
  • Just check what Conn2.Database return. – Ravi Nov 27 '18 at 07:44
  • I doubt the database is throwing up that dialog. More likely it is another application that is accessing the same database. SQL Server generally runs on a server and does not require any interaction, including dialog boxes. If something goes wrong it just adds an entry to the Windows event log.I suggest you run [SQL Server Profiler](https://stackoverflow.com/questions/10845304/how-to-trace-with-sql-server-profiler) to see what is going on exactly. – John Wu Nov 27 '18 at 07:48
  • @Ravi It is the actual database which needs to be deleted on user request. – Najjaf Nov 27 '18 at 07:54
  • @JohnWu I checked SQL Profiler, nothing wrong observed there. Query formed "alter database [TestDB] set single_user with rollback immediate; drop database [TestDB];" And When i executed in SQL Server query editor, It shows below output, Nonqualified transactions are being rolled back. Estimated rollback completion: 0%. Nonqualified transactions are being rolled back. Estimated rollback completion: 100%. – Najjaf Nov 27 '18 at 07:54
  • @Najjaf did Database get drop? – Ravi Nov 27 '18 at 07:56
  • @Ravi Yes it gets dropped but alert is also pop up. – Najjaf Nov 27 '18 at 08:49

0 Answers0