vb.net RESTORE DATABASE 异常终止

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39576389/
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:04:17  来源:igfitidea点击:

RESTORE DATABASE is terminating abnormally

sql-servervb.netrestore

提问by

I've managed to write a function that allows me to backup a SQL Server database, but I'm struggling with trying with restoring and using the backed up version.

我已经设法编写了一个允许我备份 SQL Server 数据库的函数,但我正在努力尝试恢复和使用备份版本。

My code:

我的代码:

    Try
        Dim confirmBackUp As MsgBoxResult
        confirmBackUp = MsgBox("Are you sure you want to restore?")

        If confirmBackUp = MsgBoxResult.Yes Then  Else Exit Sub
        Dim cmd As New OleDbCommand
        con = New OleDbConnection()

             ' con.Connectionstring is read from an .ini file, but the string is correct

        con.Open()
        cmd.CommandType = CommandType.Text
        cmd.CommandText = "RESTORE DATABASE MaintenanceControl FROM DISK='c:\Program Files\Microsoft SQL Server\MSSQL10_50.SQLEXPRESS\MSSQL\Backup\MaintenanceControl.bak'"
        cmd.Connection = con
        cmd.ExecuteNonQuery()

        MsgBox("Database Restored", MsgBoxStyle.OkOnly, "Success")
        con.Close()

    Catch ex As Exception
        errorLog(ex.Message, ex.StackTrace)
        MsgBox("Could not restore database, refer to error log")

    End Try

But on the .ExecuteNonQuery()line, I get the following error;

但上.ExecuteNonQuery()线时,我收到以下错误;

RESTORE DATABASE is terminating abnormally.

RESTORE cannot process database 'MaintenanceControl' because it is in use by this session. It is recommended that the master database be used when performing this operation.

RESTORE DATABASE 异常终止。

RESTORE 无法处理数据库“MaintenanceControl”,因为该会话正在使用它。建议在执行此操作时使用master 数据库。

What is the issue? Is it because con is also the name of the connection to the live database and it shouldn't be given a new string?

问题是什么?是不是因为 con 也是实时数据库连接的名称,不应该给它一个新字符串?

EDIT

编辑

I've now set the Initial Catalogpart of the connection to 'Master', as well as added con.Close() before con = New OleDbconnectionhowever the error I now get is

我现在已经Initial Catalog将连接的一部分设置为“Master”,并在之前添加了 con.Close()con = New OleDbconnection但是我现在得到的错误是

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

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

采纳答案by Joe Enos

If you're ok with dropping and recreating the database, you can use this statement, which will allow you to drop it:

如果您可以删除并重新创建数据库,则可以使用以下语句,这将允许您删除它:

alter database MaintenanceControl set single_user with rollback immediate; 
drop database SomeDatMaintenanceControl base;

and then create the database from scratch as you need.

然后根据需要从头开始创建数据库。

Dropping the database will destroy the database so that there's no possibility that someone is connected to it while you're trying to restore. Then you create the database cleanly.

删除数据库将破坏数据库,因此在您尝试恢复时不可能有人连接到它。然后干净地创建数据库。