1

It is a follow-up question to my previous question in the same forum.

I would like to take a backup of my SQL Server database. Here is the code, for the backup in C#.

userConn = new SqlConnection(userdatabase);
userConn.Open();

string UserString;

UserString = "BACKUP DATABASE @DBName TO  DISK = @FilePath";

String destPath = DestDirectory + "\\UserDataTable.bak";
SqlCommand cmd = new SqlCommand(UserString, userConn);

cmd.Parameters.AddWithValue("@dbName", userConn.Database);
cmd.Parameters.AddWithValue("@FilePath", destPath);

cmd.ExecuteNonQuery();
cmd.Dispose();

However, it throws an SQLException,

"Cannot open backup device 'D:\BookKeeping\Database\11_01_2013_21_15\Database\UserDataTable.bak'. Operating system error 3(failed to retrieve text for this error. Reason: 15105). BACKUP DATABASE is terminating abnormally."

Any Idea, what could be wrong ?

Thanks a lot for your time and your help.

Pondlife
  • 15,992
  • 6
  • 37
  • 51
Kiran
  • 8,034
  • 36
  • 110
  • 176
  • What user runs this program? – cheesemacfly Jan 11 '13 at 15:59
  • **Possible duplicate** http://stackoverflow.com/questions/2398385/sql-server-2008-backup-error-operating-system-error-5failed-to-retrieve-text – Mate Jan 11 '13 at 16:00
  • 1
    Are you tried to run query `BACKUP DATABASE @DBName TO DISK = 'D:\BookKeeping\Database\11_01_2013_21_15\Database\UserDataTable.bak'` on server, using, for instance, SSMS? – Hamlet Hakobyan Jan 11 '13 at 16:01
  • 1
    Error 3 means "not found". Does the directory exist? – Richard Deeming Jan 11 '13 at 16:45
  • @RichardDeeming, Thank you so much for your help. I assumed that, if Destination directory was not present, it would create it. Apparently I was work. Thanks a lot for your help. Would be glad, if you take few minutes to convert your comment to an answer so that it would be helpful for others. – Kiran Jan 11 '13 at 17:48

2 Answers2

5

"Operating system error 3" means that the directory was not found. SQL will not create the backup directory for you; you have to manually create it before running the backup command.

Richard Deeming
  • 29,830
  • 10
  • 79
  • 151
1

Make sure your SqlServer and the location where you want to create a backup is the same system. If you are using sqlServer remotely(Not located in your system) then you can not create a backup in your machine or you can not restore the database taking a .bak from your machine also.

Farhad
  • 4,119
  • 8
  • 43
  • 66
Subha Das
  • 11
  • 2
  • Please see this first [how-to-answer](https://stackoverflow.com/help/how-to-answer) – always-a-learner Jul 12 '17 at 05:45
  • I am not getting what I wrote wrong. But still I would like to say thank you for this proposal. I will read this . Thanks a lot. – Subha Das Jul 12 '17 at 06:32
  • This question is answered before, obviously, you can add your answer here. But You Need to understand some points before answering. First, don't add an answer which is previously added with the same code or suggestion. Second, don't add an overly complicated answer if the user has asked very specifically about the problem and what he needs to solve this. Third, You can add a comment if you want to suggest anything regarding the answer or question. – always-a-learner Jul 12 '17 at 06:48