| |

VerySource

 Forgot password?
 Register
Search
View: 724|Reply: 5

vb.net recovery sql server database problem

[Copy link]

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 Canada

Post time: 2020-1-7 01:10:01
| Show all posts |Read mode
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 ()
Reply

Use magic Report

0

Threads

8

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-1-11 13:27:01
| Show all posts
ExecuteReader is usually used less often.
But it feels that data_reader.Close () has been closed, so how can you execute it.
Reply

Use magic Report

1

Threads

6

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-1-11 14:45:02
| Show all posts
data_reader.Close ()
Put outside
Reply

Use magic Report

0

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-1-18 10:00:02
| Show all posts
Correct.
Reply

Use magic Report

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 Canada

 Author| Post time: 2020-3-13 19:45:01
| Show all posts
I tried what everyone said, and it still does n’t work, I will get an error saying that "the exception of type" System.InvalidOperationException "for processing" appears in system.data.dll, other information: there is already opened DataReader associated with this connection , You must first close it. "

So still put data_reader.Close () in it.

Please give pointers to help ~~
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 United States

Post time: 2020-3-17 16:30:02
| Show all posts
Look here,

http://community.csdn.net/Expert/topic/4751/4751771.xml?temp=.3614618
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list