|
I want to restore the database from the specified file. I close the user process first, but when I run to "Kill @spid", I get an error. "An exception of type System.Data.SqlClient.SqlException for processing appears in system.data.dll. Other information: system error". I used MSGBOX to judge that the program has taken the value of data_reader.Item (0) as 53. Running "Kill 53" directly in the query analyzer is also completely normal. Please give pointers to help ~~
The specific procedures are as follows:
Dim connect_string As String = "Data Source = SUN; Initial catalog = Master; user id = sa; persist security info = True"
Dim sql_command As New SqlCommand ()
Dim data_reader As SqlDataReader
sql_connection = New SqlConnection (connect_string)
'Close the user process, so that the database is successfully restored
sql_command.CommandText = "Select spid From Master..sysprocesses where dbid = db_id ('DBName')"
sql_command.Connection = sql_connection
sql_connection.Open ()
data_reader = sql_command.ExecuteReader
If data_reader.Read () Then
sql_command.CommandText = "Kill @spid"
sql_command.Parameters.Add ("@ spid", data_reader.Item (0))
data_reader.Close ()
sql_command.ExecuteNonQuery () ‘Here is an error
End If
'Recover database
sql_command.CommandText = "Restore database [DBName] From disk = 'C:\a.bak' With Replace"
sql_command.ExecuteNonQuery () |
|