还原数据库功能无法正常还原

时间:2016-09-21 08:52:34

标签: sql-server vb.net restore

我已编写以下代码以允许用户恢复已制作的系统的最新备份。

  Private Sub RestoreLastBackupToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles RestoreLastBackupToolStripMenuItem.Click

    Dim tr As OleDbTransaction = Nothing

    Try
        Dim confirmBackUp As MsgBoxResult
        confirmBackUp = MsgBox("Are you sure? This will restore the database to the previously backed up version", MsgBoxStyle.Critical + MsgBoxStyle.YesNo, "Confirm")

        If confirmBackUp = MsgBoxResult.Yes Then  Else Exit Sub
        Dim cmd As New OleDbCommand

        con.Close()

        Dim rCon As New OleDbConnection("My connection string is in here")

        rCon.Open()
        tr = rCon.BeginTransaction()

        cmd.CommandType = CommandType.Text
        cmd.CommandText = "ALTER DATABASE MaintenanceControl SET Single_user WITH rollback immediate; DROP DATABASE MaintenanceControl;"
        cmd.Connection = rCon
        cmd.Transaction = tr
        cmd.ExecuteNonQuery()

        cmd = New OleDbCommand
        cmd.CommandText = "RESTORE DATABASE MaintenanceControl FROM DISK='c:\Program Files\Microsoft SQL Server\MSSQL10_50.SQLEXPRESS\MSSQL\Backup\MaintenanceControl.bak'"
        cmd.Connection = rCon
        cmd.Transaction = tr
        cmd.ExecuteNonQuery()

        MsgBox("Database Restored", MsgBoxStyle.OkOnly, "Success")
        tr.Commit()
        rCon.Close()
        openCon()

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

    End Try
End Sub

此代码的问题在于,当数据库正在恢复时(在备份到同一路径后,它不会还原该数据库的最新版本,它会恢复不同的版本。)关闭并重新打开应用程序,它仍然没有使用正确的版本,获得正确版本(最新备份)的唯一方法是使用SQL Server本身的还原功能来恢复它。 / p>

为什么不能正常恢复?有没有我错过或做错的事情?

0 个答案:

没有答案