【问题标题】:exclusive access could not be obtained无法获得独占访问权
【发布时间】:2018-07-30 10:45:51
【问题描述】:

我试图在 vb.net 中恢复我的备份数据库,但我遇到了这个问题,这是什么问题?

This is the image of error

这是我正在使用的代码。

Private Sub btnRestore_Click(sender As Object, e As EventArgs) Handles btnRestore.Click
    Try
        Dim con2 As New SqlConnection
        Dim cmd2 As New SqlCommand
        Dim Filename2 As String
        Dim strQuery2 As String
        Dim database2 As String
        Dim get_servername2 As String
        'get value in selected database name dropdown menu in combo box
        database2 = cmbdatabase.Text
        get_servername2 = cmbservername.Text.Trim 'selecting servername
        Dim opendlg As New OpenFileDialog
        Dim constr2 As String
        constr2 = "Data Source=" & get_servername2 & ";Initial Catalog=master; Integrated Security= True "
        If opendlg.ShowDialog = Windows.Forms.DialogResult.OK Then
            Me.Cursor = Cursors.WaitCursor
            con2 = New SqlConnection(constr2)
            con2.Open()
            Filename2 = opendlg.FileName
            strQuery2 = " Restore database " & database2 & " from disk = '" & Filename2 & "'"

            'exe command
            Try
                cmd2 = New SqlCommand(strQuery2, con2)
                cmd2.ExecuteNonQuery()
                MessageBox.Show("Database " & database2 & " has been restored successfully. ", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information)
                con2.Close()
                Me.Cursor = Cursors.Default
            Catch ex As Exception
                Me.Cursor = Cursors.Default
                MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End Try
        End If
    Catch ex As Exception
        Me.Cursor = Cursors.Default
        MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Information)
    End Try
End Sub

【问题讨论】:

  • 您应该在此处接受答案并创建一个新问题,而不是更改(已回答)问题中的错误消息。

标签: vb.net sql-server-2012 database-restore


【解决方案1】:

您的连接字符串使用数据库Payroll 作为Intial Catalog

如消息中所述,建议在执行此操作时使用master-database。

Initial Catalog 更改为master,它应该可以工作:

constr2 = "Data Source=" & get_servername2 & ";Initial Catalog=master; Integrated Security=True"

【讨论】:

  • 如果您完全省略Initial Catalog 属性,那么我相信假定主数据库。并不是说明确是错误的。
  • @jmcilhinney 是的,也不是。取决于用户的DEFAULT_DATABASE-option 的配置。如果没有显式设置,则默认使用master
猜你喜欢
  • 1970-01-01
  • 2016-08-05
  • 2013-10-22
  • 1970-01-01
  • 2011-05-02
  • 1970-01-01
  • 2014-04-08
  • 2016-07-14
  • 1970-01-01
相关资源
最近更新 更多