Hi guys. I'm trying to restore the database using this code:
But I keep getting this error: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
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:
Anyone know how to fix this?Code:con.Open()
cmd = New SqlCommand("ALTER DATABASE CIS SET SINGLE_USER WITH ROLLBACK IMMEDIATE", con)
cmd.Connection = con
cmd.ExecuteNonQuery()
