I'm using following code to back up a SQL Server database using VB.Net to some other physical location. But I get the following error when trying to save it inside clients PCs. In some clients, it works on external hard drives but not on all clients.
Cannot open backup device 'D:\11-16-2020_POS_DB_MM.bak'. Operating system error 5(Access is denied.)
BACKUP DATABASE is terminating abnormally.
I understand it's an error happening due to user permissions. Is it possible to rectify this in my code or do I always have to set user permissions in every client (database is in a server and there are multiple clients. I use a setup package to deploy on clients)
Public Class frmBackup
Private connString As String = "Data Source=SQLSERVER\SLMA;Database=master;User Id=sa;Password =" & dbPwd & ";"
Private con As SqlConnection = New SqlConnection(connString)
Private Sub cmbbackup_Click(sender As Object, e As EventArgs) Handles cmbbackup.Click
Try
SaveFileDialog1.FileName = DateAndTime.DateString + "_" + dbName
SaveFileDialog1.Filter = "SQL Server database backup files|*.bak"
SaveFileDialog1.ShowDialog()
Dim cmd As New SqlCommand("BACKUP DATABASE " & dbName & " To disk='" & SaveFileDialog1.FileName & "'", conn)
If Not conn.State = ConnectionState.Open Then
conn.Open()
End If
cmd.ExecuteNonQuery()
conn.Close()
MsgBox("The database was successfully backup to - " & System.IO.Path.GetFullPath(OpenFileDialog1.FileName))
Catch ex As Exception
MsgBox(Err.Description)
Me.Close()
End Try
End Sub
End Class