SMO restore failed / fail to open backup device error

I got an error when I try to restore my SQL Server 2008 R2 database:

Recovery failed for server "KIMO-PC".

Used code:

    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 are the details of the exception

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

      

+3


source to share


2 answers


Take a look at the inner stack trace exception:

Unable to open backup device 'C: \ Users \ kimo \ Desktop \ POS.bak'. operating system error 5



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

+2


source


Try the following:



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

      

0


source







All Articles