使用 C# 还原 SQL 服务器数据库
Restore SQL Server database using C#
我正在尝试使用此代码恢复 SQL 数据库备份
private void btnRestore_Click(object sender, EventArgs e)
{
string dbName = "BakodahDB";
try
{
Server dbServer = new Server(); //local using windows athuentication
Restore dbRestore = new Restore() { Database = dbName, Action = RestoreActionType.Database, ReplaceDatabase = true, NoRecovery = false };
dbRestore.Devices.AddDevice(@"C:\WorkHours\dbBackup.bak", DeviceType.File);
dbRestore.PercentComplete += DbRestore_PercentComplete;
dbRestore.Complete += DbRestore_Complete;
dbRestore.SqlRestoreAsync(dbServer);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
当我点击按钮时没有任何反应,甚至没有错误消息...正确的方法是什么?
我只需要在恢复之前终止所有进程
// Kill all processes
dbServer.KillAllProcesses(dbRestore.Database);
// Set single-user mode
Database db = dbServer.Databases[dbRestore.Database];
// db.DatabaseOptions.UserAccess=true;
db.Alter(TerminationClause.RollbackTransactionsImmediately);
// Detach database
dbServer.DetachDatabase(dbRestore.Database, false);
成功了!
我正在尝试使用此代码恢复 SQL 数据库备份
private void btnRestore_Click(object sender, EventArgs e)
{
string dbName = "BakodahDB";
try
{
Server dbServer = new Server(); //local using windows athuentication
Restore dbRestore = new Restore() { Database = dbName, Action = RestoreActionType.Database, ReplaceDatabase = true, NoRecovery = false };
dbRestore.Devices.AddDevice(@"C:\WorkHours\dbBackup.bak", DeviceType.File);
dbRestore.PercentComplete += DbRestore_PercentComplete;
dbRestore.Complete += DbRestore_Complete;
dbRestore.SqlRestoreAsync(dbServer);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
当我点击按钮时没有任何反应,甚至没有错误消息...正确的方法是什么?
我只需要在恢复之前终止所有进程
// Kill all processes
dbServer.KillAllProcesses(dbRestore.Database);
// Set single-user mode
Database db = dbServer.Databases[dbRestore.Database];
// db.DatabaseOptions.UserAccess=true;
db.Alter(TerminationClause.RollbackTransactionsImmediately);
// Detach database
dbServer.DetachDatabase(dbRestore.Database, false);
成功了!