使用 VB.NET 在 SQL LocalDB 中恢复数据库
Restore database in SQL LocalDB using VB.NET
我在 VB.NET 和 SQL Server 2012 LocalDB (v11) 中开发了一个项目,我的应用程序需要 backup/restore 工具。备份部分已完成,但我卡在了恢复部分。这是我想要处理的查询(并且它在 SQL 编辑器中工作正常)
ALTER DATABASE [<.MDF File Path>]
SET SINGLE_USER WITH ROLLBACK IMMEDIATE
RESTORE DATABASE [<.MDF File Path] FROM DISK='<.bak File Path'
这里是 VB.NET 中的代码,我正在尝试执行
Sub Restorequery(ByVal que As String)
MainForm.Conn.Close()
con = New SqlConnection("Data Source=(LocalDB)\v11.0;Database=Master;integrated security=True;")
If Not con.State = ConnectionState.Open Then con.Open()
cmd = New SqlCommand(que, con)
cmd.ExecuteNonQuery()
End Sub
这是我目前尝试过的方法
使用与上面相同的查询
Restorequery("ALTER DATABASE [<.MDF File Path>] SET SINGLE_USER WITH ROLLBACK IMMEDIATE")
Restorequery("restore database [<.MDF File Path>] from disk='<.bak File Path>'")
这会导致错误
Exclusive access could not be obtained because the database is in use. RESTORE DATABASE is terminating abnormally.
在找到上述问题的原因后(即使在使用 master
数据库打开连接并使用 MainForm.Conn.Close()
关闭所有先前打开的连接之后也是无稽之谈),我尝试了第二次根据 Whosebug 中的一些链接和参考的方法。这是我尝试过的查询:
Restorequery("use [master] ")
Restorequery("alter database [<.MDF File Name>] set single_user with rollback immediate")
Restorequery("restore database[<.MDF File Name>] from disk='<.bak File Name>'")
Restorequery("alter database [<.MDF File Name>] set multi_user")
Restorequery("use [<.MDF File Name>]")
这是我在执行第二个查询时遇到的错误:
Additional information: User does not have permission to alter database <.MDF File Name with Path>, the database does not exist, or the database is not in a state that allows access checks. ALTER DATABASE statement failed.
是否有任何其他方法可以使用 VB.NET 恢复 SQL 服务器 LocalDB?
我在我的一个项目中使用过这段代码。
Try
With OpenFileDialog1
.Filter = ("DB Backup File|*.bak;")
.FilterIndex = 4
End With
OpenFileDialog1.FileName = ""
If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
Cursor = Cursors.WaitCursor
SqlConnection.ClearAllPools()
con = New SqlConnection(cs)
con.Open()
Dim cb As String = "USE Master ALTER DATABASE [" & System.Windows.Forms.Application.StartupPath & "\BRH.mdf] SET Single_User WITH Rollback Immediate Restore database [" & System.Windows.Forms.Application.StartupPath & "\BRH.mdf] FROM disk='" & OpenFileDialog1.FileName & "' WITH REPLACE ALTER DATABASE [" & System.Windows.Forms.Application.StartupPath & "\BRH.mdf] SET Multi_User "
cmd = New SqlCommand(cb)
cmd.Connection = con
cmd.ExecuteReader()
con.Close()
End If
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
我在 VB.NET 和 SQL Server 2012 LocalDB (v11) 中开发了一个项目,我的应用程序需要 backup/restore 工具。备份部分已完成,但我卡在了恢复部分。这是我想要处理的查询(并且它在 SQL 编辑器中工作正常)
ALTER DATABASE [<.MDF File Path>]
SET SINGLE_USER WITH ROLLBACK IMMEDIATE
RESTORE DATABASE [<.MDF File Path] FROM DISK='<.bak File Path'
这里是 VB.NET 中的代码,我正在尝试执行
Sub Restorequery(ByVal que As String)
MainForm.Conn.Close()
con = New SqlConnection("Data Source=(LocalDB)\v11.0;Database=Master;integrated security=True;")
If Not con.State = ConnectionState.Open Then con.Open()
cmd = New SqlCommand(que, con)
cmd.ExecuteNonQuery()
End Sub
这是我目前尝试过的方法
使用与上面相同的查询
Restorequery("ALTER DATABASE [<.MDF File Path>] SET SINGLE_USER WITH ROLLBACK IMMEDIATE") Restorequery("restore database [<.MDF File Path>] from disk='<.bak File Path>'")
这会导致错误
Exclusive access could not be obtained because the database is in use. RESTORE DATABASE is terminating abnormally.
在找到上述问题的原因后(即使在使用
master
数据库打开连接并使用MainForm.Conn.Close()
关闭所有先前打开的连接之后也是无稽之谈),我尝试了第二次根据 Whosebug 中的一些链接和参考的方法。这是我尝试过的查询:Restorequery("use [master] ") Restorequery("alter database [<.MDF File Name>] set single_user with rollback immediate") Restorequery("restore database[<.MDF File Name>] from disk='<.bak File Name>'") Restorequery("alter database [<.MDF File Name>] set multi_user") Restorequery("use [<.MDF File Name>]")
这是我在执行第二个查询时遇到的错误:
Additional information: User does not have permission to alter database <.MDF File Name with Path>, the database does not exist, or the database is not in a state that allows access checks. ALTER DATABASE statement failed.
是否有任何其他方法可以使用 VB.NET 恢复 SQL 服务器 LocalDB?
我在我的一个项目中使用过这段代码。
Try
With OpenFileDialog1
.Filter = ("DB Backup File|*.bak;")
.FilterIndex = 4
End With
OpenFileDialog1.FileName = ""
If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
Cursor = Cursors.WaitCursor
SqlConnection.ClearAllPools()
con = New SqlConnection(cs)
con.Open()
Dim cb As String = "USE Master ALTER DATABASE [" & System.Windows.Forms.Application.StartupPath & "\BRH.mdf] SET Single_User WITH Rollback Immediate Restore database [" & System.Windows.Forms.Application.StartupPath & "\BRH.mdf] FROM disk='" & OpenFileDialog1.FileName & "' WITH REPLACE ALTER DATABASE [" & System.Windows.Forms.Application.StartupPath & "\BRH.mdf] SET Multi_User "
cmd = New SqlCommand(cb)
cmd.Connection = con
cmd.ExecuteReader()
con.Close()
End If
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try