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.
User contributions licensed under CC BY-SA 3.0