SqlException (0x80131904): Cannot open backup device Operating system error 5(Access is denied.)

0

Hy I am getting sqlException while trying to backup SQL Server database programatically in ASP.NET core in hosted application. Backup works properly on local maching but after deploying it is throwing SQL exception. My code is

string fileName = Guid.NewGuid().ToString()+".bak";
var filePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot","DatabaseBackups", fileName);               
System.Console.WriteLine(filePath);
string connectionString = configuration.GetConnectionString("DefaultConnection");
var query = $"BACKUP DATABASE MySchool TO DISK='{filePath}'";
using ( SqlConnection conn = new SqlConnection(connectionString))
{
 conn.Open();
 var command = new SqlCommand(query, conn);
 command.ExecuteNonQuery();
 Logger.LogInformation("BackUp Completed");
 conn.Close();
}

If its permission issue on the server, I have given full permission to wwwwroot folder in server.

c#
sql-server
asp.net-core
asked on Stack Overflow Nov 10, 2020 by zombie551 • edited Nov 10, 2020 by zombie551

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0