【问题标题】:RESTORE DATABASE is terminating abnormallyRESTORE DATABASE 异常终止
【发布时间】:2016-09-20 00:28:32
【问题描述】:

我设法编写了一个允许我备份 SQL Server 数据库的函数,但我在尝试恢复和使用备份版本时遇到了困难。

我的代码:

    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

但在.ExecuteNonQuery() 行,我收到以下错误;

RESTORE DATABASE 异常终止。

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

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

编辑

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

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

【问题讨论】:

  • 您的连接字符串是否也指向数据库 MaintenanceControl?在您连接的数据库上执行恢复可能是问题所在 - 错误消息似乎确实表明了这一点。
  • 你试过Use master;go;cmd.CommandText = "RESTORE DATABASE MaintenanceCo.....吗?
  • @phillyd 打开程序时,con 指向的是 MaintenanceControl 的实时版本,是的
  • @artm 那里有一些引号或缺少的东西吗?按原样粘贴它希望我生成用于使用和主控的方法存根
  • 我认为将您的连接字符串改为指向主数据库可能会让您恢复 MaintenanceControl。

标签: sql-server vb.net restore


【解决方案1】:

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

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

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

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

【讨论】:

    猜你喜欢
    • 2022-07-06
    • 1970-01-01
    • 2011-03-03
    • 2012-07-07
    • 2017-08-13
    • 1970-01-01
    • 2013-10-14
    • 1970-01-01
    • 2014-10-08
    相关资源
    最近更新 更多