我正在尝试以编程方式备份我的数据库。方案是用户将单击备份按钮,将打开FolderBrowserDialog,用户可以在其中创建新文件夹或选择现有文件夹,然后按ok在该目标文件夹/目标中创建.bak文件。我写了以下代码,但获得拒绝访问(请参阅问题的标题)错误,我无法找出问题。我正在使用SQL Server Management Studio 2014。 这是代码:
Private Sub DbBackupButton_Click(sender As Object, e As EventArgs) Handles DbBackupButton.Click
If FolderBrowserDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
Try
Dim DatabaseName As String = "MyDatabaseName"
Dim DestinationPath As String = FolderBrowserDialog1.SelectedPath & "MyDatabaseName" & DateTime.Now.Day & "-" & DateTime.Now.Month & "-" & DateTime.Now.Year & ".bak"
backupDB(DatabaseName, DestinationPath)
Catch ex As ApplicationException
MsgBox("ERROR" & ex.Message, "ERROR")
End Try
End If
End Sub
Private Sub backupDB(ByVal DatabaseName As String, ByVal DestinationPath As String)
Dim connString As String = ConfigurationManager.ConnectionStrings("dbx").ConnectionString
Using conn As New SqlConnection(connString)
Using cmd As New SqlCommand("BACKUP DATABASE @DatabaseName TO DISK = @DestinationPath", conn)
conn.Open()
cmd.Parameters.AddWithValue("@DatabaseName", DatabaseName)
cmd.Parameters.AddWithValue("@DestinationPath", DestinationPath)
cmd.ExecuteNonQuery()
MessageBox.Show("Backup Generated Succesfully", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Using
End Using
End Sub
任何人都可以找出问题及其解决方案吗?感谢