Droping Sql-Server Database through c#

camachb

New Member
I am using the below code to delete a Database through C#\[code\]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;\[/code\]But I get the Database currently in use exception. Can any one Help?
 
Back
Top