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();
dialog.ShowDialog();
if (dialog.SelectedPath != "")
{
result = bLL.backUp(dialog.SelectedPath);
if (result == "true")
{
MessageBox.Show(message.messageSettingBackUpDataBaseSuccess(), "",
MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1, message.messageBoxAlign());
}
else
{
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);
try
{
Connection.Open();
Command.ExecuteNonQuery();
Connection.Close();
return "true";
}
catch (Exception ex)
{
return ex.ToString();
}
}
}