Search code examples
winformsc#-4.0sql-server-2008-r2smo

SMO Restore Failed/Cannot Open Backup Device Error


I got an error while I'm trying to restore a SQL Server 2008 R2 database:

Restore failed for Server 'KIMO-PC'.

Code used:

    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);

and here is the exception details

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

Solution

  • Take a look at the stack trace inner exception:

    Cannot open backup device 'C:\Users\kimo\Desktop\POS.bak'. Operating system error 5

    Most likely, the SQL Server does not have rights to view files in kimo\Desktop. Move the backup to a different folder like c:\temp and try it again.