I currently have the following command
SqlCommand command = new SqlCommand(@"sys.sp_detach_db 'DBname'", conn);
to separate the database, but when it is executed, it throws an exception. A statement that the database is being used. How can I remove a connection when or when I disconnect it?
Update: I am currently using SMO, but it still does not work:
bool DetachBackup(string backupDBName)
{
string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
var builder = new SqlConnectionStringBuilder(connectionString);
string serverName = builder.DataSource;
string dbName = builder.InitialCatalog;
try
{
Server smoServer = new Server(serverName);
smoServer.DetachDatabase(backupDBName + DateTime.Now.ToString("yyyyMMdd"), false);
return true;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
return false;
}
}
source
share