To detach the database and solve the error at the same time
Cannot detach database "YOUR_DATABASE" because it is currently in use
you can just use the following code:
private void DetachDatabase()
{
String databaseConnectionString = "Data Source=localhost;MultipleActiveResultSets=True;Integrated Security=True";
using (SqlConnection sqlDatabaseConnection = new SqlConnection(databaseConnectionString))
{
try
{
sqlDatabaseConnection.Open();
string commandString = "ALTER DATABASE YOUR_DATABASE SET OFFLINE WITH ROLLBACK IMMEDIATE ALTER DATABASE YOUR_DATABASE SET SINGLE_USER EXEC sp_detach_db 'YOUR_DATABASE'";
SqlCommand sqlDatabaseCommand = new SqlCommand(commandString, sqlDatabaseConnection);
sqlDatabaseCommand.ExecuteNonQuery();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
, YOUR_DATABASE , . YOUR_DATABASE .mdf , ...
-:
: SQL Server -
, SQL SERVER 2014