Results 1 to 2 of 2

Thread: Restore database error

  1. #1

    Thread Starter
    Addicted Member riechan's Avatar
    Join Date
    Feb 2008
    Location
    Japan
    Posts
    254

    Restore database error

    Hi guys. I'm trying to restore the database using this code:

    Code:
            Dim con As SqlConnection = New SqlConnection("Data Source=localhost; Integrated Security=SSPI; Initial Catalog=master")
            Dim cmd As SqlCommand
    
            If (MsgBox("Restore Data?", MsgBoxStyle.YesNo, "CSIS Data Backup")) = MsgBoxResult.Yes Then
                If (MsgBox("Restoring data requires that the application be closed after restore process. Continue?", MsgBoxStyle.YesNo, "CSIS Data Backup")) = MsgBoxResult.Yes Then
    
                    con.Open()
                    cmd = New SqlCommand("ALTER DATABASE CIS SET SINGLE_USER WITH ROLLBACK IMMEDIATE", con)
                    cmd.Connection = con
                    cmd.ExecuteNonQuery()
    
                    cmd = New SqlCommand("RESTORE DATABASE CIS FROM DISK = 'C:\\CIS.bak';")
                    cmd.Connection = con
                    cmd.ExecuteNonQuery()
    
                    cmd = New SqlCommand("ALTER DATABASE CIS SET MULTI_USER WITH ROLLBACK IMMEDIATE;", con)
                    cmd.Connection = con
                    cmd.ExecuteNonQuery()
    
                    MsgBox("Data has been successfully restored. Application will now close.", MsgBoxStyle.Information, "Data Restore Successful")
                    con.Close()
    
                    End
                Else
                    Exit Sub
                End If
            Else
                Exit Sub
            End If
    But I keep getting this error:

    Changes to the state or options of database 'CIS' cannot be made at this time. The database is in single-user mode, and a user is currently connected to it. ALTER DATABASE statement failed.

    And it points to this code block, and fails to execute the query:

    Code:
    con.Open()
                    cmd = New SqlCommand("ALTER DATABASE CIS SET SINGLE_USER WITH ROLLBACK IMMEDIATE", con)
                    cmd.Connection = con
                    cmd.ExecuteNonQuery()
    Anyone know how to fix this?
    ====================
    ほんとにどもありがとう!

    Rie Ishida

  2. #2
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Restore database error

    You need to change your connection to another database on the server inorder to do a restore. No one can be connected to the database if it is being restored.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width