I am trying to backup my DB using this code First time I got it to run it worked well and My DB was backed up
Since then I am getting 'Access Denied' errors at the executeNonQuery line . I deleted the old backup in case it just didn't like overwriting it but that didn't help
The exact error is ---
"Cannot open backup device 'C:\Program Files\Microsoft SQL Server\MSSQL14.SQLEXPRESS17\MSSQL\Backup'. Operating system error 5(Access is denied.).
BACKUP DATABASE is terminating abnormally."
any advice would be appreciated
Since then I am getting 'Access Denied' errors at the executeNonQuery line . I deleted the old backup in case it just didn't like overwriting it but that didn't help
The exact error is ---
"Cannot open backup device 'C:\Program Files\Microsoft SQL Server\MSSQL14.SQLEXPRESS17\MSSQL\Backup'. Operating system error 5(Access is denied.).
BACKUP DATABASE is terminating abnormally."
any advice would be appreciated
Code:
Dim sqlConnectionString As String = $"Integrated Security=SSPI;Persist Security Info=False;Initial Catalog={DBName};Data Source={Server}"
'Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=BankAccounts;Data Source=MAIN\SQLEXPRESS17
Dim BackupLocation As String
BackupLocation = GetAllTextBeforeLastOccuranceof(DataLocation, "\") & "Backup"
Dim conn As New SqlConnection(sqlConnectionString)
conn.Open()
Dim cmd As New SqlCommand
cmd.CommandType = CommandType.Text
cmd.CommandText = $"BACKUP DATABASE BankAccounts TO DISK='{BackupLocation}'"
cmd.Connection = conn
cmd.ExecuteNonQuery()
conn.Close()