-1
我正在構建一個函數,該函數使用SQL查詢來獲取要刪除的表的列表。然後我想遍歷每個結果行並執行命令。執行SQL命令作爲SQL命令的嵌套循環
Public Shared Sub DropTables(DbConnectionString As String)
Dim sqlConnection As New SqlConnection(DbConnectionString)
Dim cmd As New SqlCommand
Dim reader As SqlDataReader
cmd.CommandText = "SELECT 'DROP TABLE ' + name + ';' from sysobjects where name like 'SM_%' and type='U';"
cmd.CommandType = CommandType.Text
cmd.Connection = sqlConnection
sqlConnection.Open()
reader = cmd.ExecuteReader()
If reader.HasRows Then
Do While reader.Read()
Using nonQuery As SqlCommand = sqlConnection.CreateCommand()
nonQuery.CommandText = reader.GetString(0)
nonQuery.ExecuteNonQuery()
End Using
Loop
Else
Console.WriteLine("No rows found.")
End If
sqlConnection.Close()
End Sub
我的問題是,我在這條線得到一個這樣的錯誤:nonQuery.ExecuteNonQuery()
There is already an open DataReader associated with this Command which must be closed first.
但是我使用的是不同的數據讀取器。
你確定該消息不是'已經有一個開放的DataReader與這個CONNECTION'關聯了嗎? – Plutonix