I'm trying to restore a database from a .BAK
file using C# and SMO. This is my code.
public static void RestoreDatabase()
{
string dbConnString = Configuration.DatabaseConnectionString;
ServerConnection connection = new ServerConnection(@"dbserver\sqlexpress", "user", "password");
Server smoServer = new Server(connection);
Restore rstDatabase = new Restore();
rstDatabase.Action = RestoreActionType.Database;
rstDatabase.Database = "AppDb";
BackupDeviceItem bkpDevice = new BackupDeviceItem(@"TestData\db-backup.bak", DeviceType.File);
rstDatabase.Devices.Add(bkpDevice);
rstDatabase.ReplaceDatabase = true;
// Kill all processes
smoServer.KillAllProcesses(rstDatabase.Database);
// Set single-user mode
Database db = smoServer.Databases[rstDatabase.Database];
db.DatabaseOptions.UserAccess = DatabaseUserAccess.Single;
db.Alter(TerminationClause.RollbackTransactionsImmediately);
rstDatabase.SqlRestore(smoServer);
}
However when I try to run this method I get the following (error) message when it attempts to kill all processes:
Cannot use KILL to kill your own process.
I would be very grateful if someone could help solve this issue.
master
instead. – Patrilinyconnection
looks like it's master because he does not specify a database name. – Gauguinuser
's default database might beAppDB
? – Patriliny