-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