使用c#代码恢复失败

本文关键字:失败 恢复 代码 使用 | 更新日期: 2023-09-27 18:13:20

我的问题是我不能使用c#代码在wpf中恢复我的数据库。错误是"为服务器'DELL/LOCALSERVER'恢复失败"。我刚刚找到了我正在使用的代码,它是这个:

//获取.bak文件路径

private void btnBrowse_Click(object sender, RoutedEventArgs e)
{
  OpenFileDialog ofd = new OpenFileDialog();
  ofd.ShowDialog();
  txtBackupPath.Text = ofd.FileName;
}
private void btnRestore_Click(object sender, RoutedEventArgs e)
{
  ServerConnection con = new ServerConnection(@server, userId, password);
  Server sqlServerInstance = new Server(con);
  Restore destination = new Restore();
  destination.Action = RestoreActionType.Database;
  destination.Database = initial_catalog;
  BackupDeviceItem source = new BackupDeviceItem(txtBackupPath.Text, DeviceType.File);
  destination.Devices.Add(source);
  destination.ReplaceDatabase = true;
  destination.SqlRestore(sqlServerInstance);
  System.Windows.Forms.MessageBox.Show("The restore of database " + "'H2RPDB'" + " completed sccessfully", "Microsoft SQL Server Management Studio", MessageBoxButtons.OK, MessageBoxIcon.Information);
  con.Disconnect();
}

我不知道对这段代码不熟悉有什么问题。谢谢你的回答。

使用c#代码恢复失败

幸好代码是正确的。只是系统没有权限访问我要访问的路径。除此之外,它可以工作。