3

我在尝试恢复 SQL Server 2008 R2 数据库时遇到错误:

服务器“KIMO-PC”的恢复失败。

使用的代码:

    Server myServer = new Server("KIMO-PC");
    Database myDatabase = new Database(myServer, "POS");
    Restore restoreDB = new Restore();
    restoreDB.Database = myDatabase.Name;
    restoreDB.Action = RestoreActionType.Database;
    restoreDB.Devices.AddDevice(FileName, DeviceType.File);

    restoreDB.ReplaceDatabase = false;

    restoreDB.NoRecovery = true;

    restoreDB.PercentComplete += CompletionStatusInPercent;
    restoreDB.Complete += Restore_Completed;

    restoreDB.SqlRestore(myServer);

这是异常详细信息

Microsoft.SqlServer.Management.Smo.FailedOperationException was unhandled HelpLink=http://go.microsoft.com/fwlink?ProdName=Microsoft+SQL+Server&ProdVer=10.50.1600.1+((KJ_RTM).100402-1540+)&EvtSrc=Microsoft.SqlServer.Management.Smo.ExceptionTemplates.FailedOperationExceptionText&EvtID=Restore+Server&LinkId=20476 Message=Restore failed for Server 'KIMO-PC'.
 Source=Microsoft.SqlServer.SmoExtended Operation=Restore 
StackTrace: at Microsoft.SqlServer.Management.Smo.Restore.SqlRestore(Server srv) at ICS.Forms.frmRestore.Restoredb(String FileName) 
  in D:\POS\POS\POS\Database\RestoreDB.cs:line 56 at ICS.Forms.frmRestore.btnBackup_Click(Object sender, EventArgs e) 
  in D:\POS\POS\POS\Database\RestoreDB.cs:line 35 at System.Windows.Forms.Control.OnClick(EventArgs e) 
  at System.Windows.Forms.Button.OnClick(EventArgs e) at System.Windows.Forms.Button.PerformClick() 
  at System.Windows.Forms.Form.ProcessDialogKey(Keys keyData) 
  at System.Windows.Forms.Control.ProcessDialogKey(Keys keyData) 
  at System.Windows.Forms.Control.PreProcessMessage(Message& msg) 
  at System.Windows.Forms.Control.PreProcessControlMessageInternal(Control target, Message& msg) 
  at System.Windows.Forms.Application.ThreadContext.PreTranslateMessage(MSG& msg) 
  at System.Windows.Forms.Application.ThreadContext.System.Windows.Forms.UnsafeNativeMethods.IMsoComponent.FPreTranslateMessage(MSG& msg) 
  at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData) 
  at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) 
  at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) 
  at System.Windows.Forms.Application.Run(Form mainForm) at POS.Program.Main() in D:\POS\POS\POS\Program.cs:line 18 
  at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) 
  at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) 
  at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() at System.Threading.ThreadHelper.ThreadStart_Context(Object state) 
  at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx) 
  at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart() 
InnerException: Microsoft.SqlServer.Management.Smo.SmoException HelpLink=http://go.microsoft.com/fwlink?ProdName=Microsoft+SQL+Server&ProdVer=10.50.1600.1+((KJ_RTM).100402-1540+)&LinkId=20476 
  Message=System.Data.SqlClient.SqlError: Cannot open backup device 'C:\Users\kimo\Desktop\POS.bak'. Operating system error 5(failed to retrieve text for this error. Reason: 15100). 
  Source=Microsoft.SqlServer.Smo StackTrace: 
  at Microsoft.SqlServer.Management.Smo.ExecutionManager.ExecuteNonQueryWithMessage(StringCollection queries, ServerMessageEventHandler dbccMessageHandler, Boolean errorsAsMessages) 
  at Microsoft.SqlServer.Management.Smo.BackupRestoreBase.ExecuteSql(Server server, StringCollection queries) 
  at Microsoft.SqlServer.Management.Smo.Restore.SqlRestore(Server srv) InnerException
4

2 回答 2

2

看一下堆栈跟踪内部异常:

无法打开备份设备“C:\Users\kimo\Desktop\POS.bak”。操作系统错误 5

很可能,SQL Server 无权查看 kimo\Desktop 中的文件。将备份移至其他文件夹,例如 c:\temp,然后重试。

于 2013-01-22T14:09:02.773 回答
0

尝试这个:

Database currentDb = srvsql.Databases["dbname"];// get the current database

if (currentDb != null)
{
    Connection.ChangeDatabase("dbname");
    //Stop all processes running on the DataBase database
    server.KillAllProcesses("dbname ");
}
于 2013-03-01T06:29:14.440 回答