Quantcast
Channel: VBForums
Viewing all articles
Browse latest Browse all 15064

Running code against Sqlserver worked fine first time but now 'Access denied'

$
0
0
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

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()


Viewing all articles
Browse latest Browse all 15064

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>