0

I want to restore a SQL Server .bak backup file in InnoSetup.

Here is the code I use:

try
    ExtractTemporaryFile ('mydb.bak');
    ADOConnection := CreateOleObject('ADODB.Connection');
    ADOConnection.ConnectionString := 
      'Provider=SQLOLEDB;' +               // provider
      'Data Source=DESKTOP-UE6ST1P\RMPRO;' +   // server name
      'Initial Catalog=master ;' +       // default database
      'User Id=sa;' +                // user name
      'Password=mypass;';                   // password
    
    ADOConnection.Open;
    try

      ADOCommand := CreateOleObject('ADODB.Command');
      ADOCommand.ActiveConnection := ADOConnection;
      ADOCommand.CommandText := 'RESTORE DataBase mydb FROM DISK = ' + QuotedStr(ExpandConstant('{tmp}\mydb.bak'));
      ADOCommand.CommandType := adCmdText;
      ADOCommand.Execute(NULL, NULL, adCmdText or adExecuteNoRecords);      
    finally
      ADOConnection.Close;
    end;
  except
    MsgBox('Error', mbError, MB_OK);
  end;
end;

When I run this code get this error

Exception: Microsoft OLE DB Provider for SQL Server: Cannot open backup device 'C:\Users\sina\AppData\Local\Temp\is-QADA6.tmp\mydb.bak'. Operating system error 5 (Access is denied).

How I can handle access denied in InnoSetup? I run the setup in "Run as Administrator" and turn UAC off, but I still have this problem.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
sina
  • 331
  • 4
  • 12
  • The SQL Server service is configured to run under a particular Windows/Active Directory account. Does that account have permissions to access the folder containing your .bak file? – AlwaysLearning Aug 23 '20 at 09:00
  • 1
    You **really shouldn't** do this from within a setup program: there's a million-and-one things that could go wrong (and you're experiencing one right now!) and there's no way for your to gracefully handle all of those error conditions (as users generally expect an installation (and especially uninstallation) to always succeed provided they have UAC permission. Instead your application code should do this during first-run initialization or with a dedicated setup utility. – Dai Aug 23 '20 at 09:14
  • 2
    The Service Account should *not* have access to a user's User directory. Put the file so where it does (and should) have access to. – Thom A Aug 23 '20 at 09:32

0 Answers0