Scorpio
Scorpio

Reputation: 153

Database backup error

I have this code snippet that creates backup of db:

try
{
  string connStr = "<valid connection string>";

  using (SqlConnection conn = new SqlConnection(connStr))
  {
    conn.Open();

    string sqlStmt = String.Format("BACKUP DATABASE InventoryDB TO DISK='{0}'", 
                                      DBBackupSaveFileDialog.FileName);

    using (SqlCommand bu2 = new SqlCommand(sqlStmt, conn))
    {
      bu2.ExecuteNonQuery();
    }
...

problem is that it works fine on developer pc but when i try on different machine it will give this error

Cannot open backup device 'C:\User\Saleh\Documents\12.bak'. Operating system error 5 (failed to rerieve text for this error. Reason: 15105).

Upvotes: 0

Views: 433

Answers (1)

Aaron Bertrand
Aaron Bertrand

Reputation: 280644

You need to make sure the SQL Server service account has write access to C:\Users\Saleh\

First, see what account SQL Server is running as. Control Panel > Administrative Tools > Services, right-click the relevant SQL Server service, hit Properties, and see the account listed on the Log On tab. Now go to Windows Explorer, right-click the folder, hit Properties, and on the Security tab, you can add this user account to the list, and make sure they have write access. If you have further problems with this, your question should probably to to superuser.com as it's becoming a Windows / permissions problem, not a programming one.

Better yet, stop running backups to user folders - use SQL Server's backup folder, which SQL Server already has permissions to write to. If you need to move it later, do that separately.

Upvotes: 2

Related Questions