0

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

4
  • 1
    Please take care of those users that take out time to resolve your issues free of cost. Please encourage them by upvote/post accept Commented Aug 30, 2012 at 17:40
  • Your code is open to SQL Injection, at least in theory - FileOpenDialog's validation might prevent it, but I'd take no risks. Also Consider using SMO Backup class instead. Commented Aug 30, 2012 at 17:40
  • Free tip of the day: hitting ctrl+c while the message box has focus copies the text to the clipboard - much easier paste text here than linking/embedding images. Commented Aug 30, 2012 at 17:44
  • @Christian.K I added the same advice to my edit. :-) Commented Aug 30, 2012 at 17:47

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Changed backup path to what? Another folder the SQL Server service account doesn't have permissions to write to?
You're not answering my question. Am I supposed to guess what service account SQL Server is running as, and whether you've applied appropriate permissions to d:\partition\?
Just another vote for not backing up to a user folder. Reasoning: say sys admin comes along and Saleh has quit his/her job. They see the account still kicking around on the computer and delete it, or disable the user etc. Will your backups still work? Will other users have to go to Saleh to get the backups back? There is a standard backup folder and it is good to use it if for not other reason then that a new DBA will look there for the backups.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.