|
我要从指定文件恢复数据库,先关闭用户进程,但是运行到“Kill @spid”就报错“为处理的System.Data.SqlClient.SqlException 类型的异常出现在 system.data.dll 中。其他信息:系统错误”。我用MSGBOX判断程序已取到data_reader.Item(0)的值为53,直接在查询分析器里运行“Kill 53”也完全正常。请高手帮忙指点~~
具体程序如下:
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)
'关闭用户进程,使数据库成功恢复
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() ‘到这里报错
End If
'恢复数据库
sql_command.CommandText = "Restore database [DBName] From disk='C:\a.bak' With Replace"
sql_command.ExecuteNonQuery() |
|