Click here to Skip to main content
15,901,666 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want get a backup from my SQL Server database in C# Windows Forms. But I have some problems. When I want to save the backup file with a different name of database, my app crashes and errors. Also I cannot save my backup on the desktop but all off the other location not having error.

this is the error I get when I try and save the backup on the desktop; there is no problem with any other address or folder.

"System.Data.SqlClient.SqlException (0x80131904): Cannot open backup device 'C:\\Users\\sadegh\\Desktop\\Custom_Category.bak'. Operating system error 5(Access is denied.).\r\nBACKUP DATABASE is terminating abnormally.\r\n   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)\r\n   at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)\r\n   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)\r\n   at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)\r\n   at System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async, Int32 timeout, Boolean asyncWrite)\r\n   at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry)\r\n   at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()\r\n   at DataAccessLayer.BackUpRestor.backUp(String address) in C:\\Users\\sadegh\\source\\repos\\Custom_Category\\DataAccessLayer\\BackUpRestor.cs:line 22\r\nClientConnectionId:02e41dc3-d9b0-4c5c-baa1-089a79463b37\r\nError Number:3201,State:1,Class:16"

What I have tried:

This is my code for getting back up; I use FolderBrowserDialog, because any name other than the main name of the database is entered by the user, the program gets an error.

<pre>  private void ButtonBackUp_Click(object sender, EventArgs e)
            string result;
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            BLLBackupRestor bLL = new BLLBackupRestor();

            if (dialog.SelectedPath != "")
                result = bLL.backUp(dialog.SelectedPath);
                if (result == "true")
                    MessageBox.Show(message.messageSettingBackUpDataBaseSuccess(), "",
                        MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1, message.messageBoxAlign());
                    MessageBox.Show(message.messageSettingBackUpDataBaseError(), "",
                       MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1, message.messageBoxAlign());

public string backUp(string address)
           using (SqlConnection Connection = new SqlConnection(@"data source=DESKTOP-JU6B74R\SQL2019;          initial catalog=Custom_Category; integrated security=true"))
               string Query = @"backup database Custom_Category to disk = '" + address + @"\Custom_Category.bak'";
               SqlCommand Command = new SqlCommand(Query, Connection);
                   return "true";
               catch (Exception ex)

                   return ex.ToString();

Updated 12-May-23 20:51pm

1 solution

Probably, it's because the SQL DB engine doesn't run under your user - it has it's own user with different permissions because it runs as a service and doesn't require any user to be logged in.

As such, it doesn't have access to your user files so when you try to store the backup under C:\Users\sadegh\... it cannot create or edit the file.

The best ways to handle this are to have a specific folder off C:\ which has open permission for all users, or to store it under a folder under a data folder such as the CommonApplicationData folder.
This may help: Where Should I Store My Data?[^]
Share this answer
Member 11400059 13-May-23 9:05am    
so my code have not problem? thank you for your suggestion.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900