vb.net 修复 无法获得独占访问权限,因为数据库正在使用中。RESTORE DATABASE 异常终止。网络

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48135077/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 20:22:17  来源:igfitidea点击:

fix Exclusive access could not be obtained because the database is in use. RESTORE DATABASE is terminating abnormally. vb.net

vb.net

提问by Yousry Eldashash

This is my connection string:

这是我的连接字符串:

  Dim connSQLServerMaster As New SqlConnection("Data Source=YOUSRY\LAB_SERVER;Initial Catalog=master;User ID=admin;Password=123")
        Dim cmd As New SqlCommand("Restore database clinic_program from disk ='d:\backup\clinic_program.bak' with Replace,recovery", connSQLServerMaster)
        connSQLServerMaster.Open()
        cmd.ExecuteNonQuery()
       connSQLServerMaster.Close()
    end sub

When trying to access the DB there is an error:

尝试访问数据库时出现错误:

Exclusive access could not be obtained because the database is in use. RESTORE DATABASE is terminating abnormally

无法获得独占访问权限,因为数据库正在使用中。RESTORE DATABASE 异常终止

Any help would be appreciated.

任何帮助,将不胜感激。

回答by Barr J

A restore can only happen if the database does not have any connections to it (besides yours). The easy way on a MS SQL Server to kick all users off is:

只有当数据库没有任何连接(除了你的)时,还原才会发生。在 MS SQL Server 上启动所有用户的简单方法是:

ALTER DATABASE [MyDB] SET Single_User WITH Rollback Immediate
GO

Now, you can perform your restore with impunity. Make sure you set it back to Multi-user mode when you're done with the restore:

现在,您可以不受惩罚地执行恢复。完成还原后,请确保将其设置回多用户模式:

ALTER DATABASE [MyDB] SET Multi_User
GO