恢复数据库时出错 [英] Error restoring the database
本文介绍了恢复数据库时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好!我恢复数据库,但我有一个错误。此错误是服务器WIN_64-PC\SQLEXPRESS恢复失败如何纠正此错误?
这是我的代码
Hi everyone! I restore the database, but I'm having one error. This error is "Restore failed for Server WIN_64-PC\SQLEXPRESS" How do I rectify this error ?
Here is my code
public void Restore(string Servername, string DBname, string user, string pass, string path)
{
try
{
Restore restore = new Restore();
restore.Database = DBname;
restore.Action = RestoreActionType.Database;
BackupDeviceItem deviceitem = new BackupDeviceItem(path, DeviceType.File);
ServerConnection connection = new ServerConnection(Servername, user, pass);
Server server = new Server(connection);
restore.Devices.Add(deviceitem);
restore.ReplaceDatabase = true;
restore.NoRecovery = true;
restore.Complete += new ServerMessageEventHandler(Restore_Complete);
restore.PercentComplete += new PercentCompleteEventHandler(CompletionsStatusInPercent);
// perform the restore
restore.SqlRestore(server);
}
catch (SmoException ex)
{
MessageBox.Show(ex.Message);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
void Restore_Complete(object sender, ServerMessageEventArgs e)
{
MessageBox.Show("Restore Complete !");
}
void CompletionsStatusInPercent(object sender, PercentCompleteEventArgs args)
{
Console.WriteLine(args.Percent);
}
}
推荐答案
Hello Vunguyen112,
您可以使用SMO来实现此目的。以下代码片段可以帮助您入门。
Hello Vunguyen112,
You can use SMO to achieve this. Following code snippet should get you started.
Microsoft.SqlServer.Management.Smo.Server smoServer = new Server(new ServerConnection(server));
Database db = smoServer.Databases['DB_NAME'];
string dbPath = Path.Combine(db.PrimaryFilePath, 'DBFILE.MDF');
string logPath = Path.Combine(db.PrimaryFilePath, 'DB_LOG.LDF');
Restore restore = new Restore();
BackupDeviceItem deviceItem = new BackupDeviceItem('DRIVE:\MYDATABASE.BAK', DeviceType.File);
restore.Devices.Add(deviceItem);
restore.Database = backupDatabaseTo;
restore.FileNumber = restoreFileNumber;
restore.Action = RestoreActionType.Database;
restore.ReplaceDatabase = true;
restore.SqlRestore(smoServer);
db = smoServer.Databases['DB_NAME'];
db.SetOnline();
smoServer.Refresh();
db.Refresh();
您需要在项目中使用以下引用。
- Microsoft.SqlServer.Smo
- Microsoft.SqlServer.SmoExtended
- Microsoft.SqlServer.Management.Sdk.Sfc
这篇关于恢复数据库时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文