无法打开备份设备'C:\ Users \ PC \ Desktop \ DBBackupMyDatabaseName 21-12-2016.bak'。操作系统错误5(访问被拒绝。)

时间:2016-12-21 08:54:51

标签: sql-server sql-server-2014

我正在尝试以编程方式备份​​我的数据库。方案是用户将单击备份按钮,将打开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

任何人都可以找出问题及其解决方案吗?感谢

0 个答案:

没有答案