ExecuteReader 需要一个打开且可用的连接 - 当前状态为关闭
Posted
技术标签:
【中文标题】ExecuteReader 需要一个打开且可用的连接 - 当前状态为关闭【英文标题】:ExecuteReader requires an open and available Connection - current state is closed 【发布时间】:2020-02-28 20:15:25 【问题描述】:全部。 自从昨晚深夜抬起头以来,我一直在努力解决这个问题。 不知道为什么会这样,但确实如此。过去只使用 ONE Artistcon.open() 现在要求我在每个新的“ExecuteReader”上都拥有它。 下面是代码示例。从连接字符串开始,然后是实际代码。 我在下面标记了一行,以便于查找。
我试图将 ArtistCon.open() 放在代码区域中,在该区域的 select 语句上方,但它出错了,
web.config => 数据库.config
<connectionStrings>
<add name ="Local" connectionString="Data Source=ServerName\InstanceName;Database=DatabaseName;User ID=UserName;Password=********;MultipleActiveResultSets=True"/>
</connectionStrings>
default.vb.aspx
Dim Artistcon As New SqlConnection
Artistcon = New SqlConnection(ConfigurationManager.ConnectionStrings("Local").ConnectionString)
Artistcon.Open()
Dim chWriters As New SqlCommand("SELECT TrackID, WID FROM Tracks WHERE (TrackFileName = @TrackFileName)", Artistcon)
chWriters.Parameters.Add(New SqlParameter("@TrackFileName", getfile & ".mp3"))
Dim rschhWriters As SqlDataReader
rschhWriters = chWriters.ExecuteReader(Data.CommandBehavior.CloseConnection)
If rschhWriters.Read() Then
Dim sW As String = strWriters
Dim leftString = sW.Substring(0, sW.IndexOf("["))
Dim Wparts As String() = leftString.Split(New Char() "/"c)
' Loop through result strings with For Each.
Dim Wpart As String
For Each Wpart In Wparts
Dim theWriters As String = Replace(Wpart, " ", "_")
Dim getWriters As New SqlCommand("SELECT WritersID, WName FROM Writer WHERE (WName = @WName)", Artistcon)
getWriters.Parameters.Add(New SqlParameter("@WName", theWriters.TrimEnd(CChar("_"))))
Dim rsWriters As SqlDataReader
' =================ERROR LINE BELOW=================
' ExecuteReader requires an open and available Connection. The connection's current state is closed.
rsWriters = getWriters.ExecuteReader(Data.CommandBehavior.CloseConnection) <<< ERROR LINE
' =================ERROR LINE ABOVE=================
If Not rsWriters.Read() Then ' If record exist
lblWriter.Text = "Insert WName=" & Wpart
strWriters = "INSERT INTO Writer(WName)VALUES(@WName)"
WriterCMD = New SqlCommand(strWriters, Artistcon)
WriterCMD.Parameters.Add(New SqlParameter("@WName", theWriters.TrimEnd(CChar("_"))))
Try
WriterCMD.ExecuteNonQuery()
Catch ex As Exception
Finally
End Try
End If
rsWriters.Close()
getWriters.Dispose()
在我开始添加并更改代码之前,我之前没有遇到此代码的问题,然后突然之间,我开始在每个嵌套记录上都收到此错误,并且不得不将 ArtistCon.open( ) 在每个 ExecuteReader 上,然后我在嵌套语句中得到这个。
谢谢。
【问题讨论】:
试试rschhWriters = chWriters.ExecuteReader()
你要求用 Data.CommandBehavior.CloseConnection 关闭它
LarsTech。请添加您的评论作为答案。老实说,我没有注意到我是这样关闭它的。非常感谢。
【参考方案1】:
您特别要求关闭与此通话的连接:
Dim rschhWriters As SqlDataReader
rschhWriters = chWriters.ExecuteReader(Data.CommandBehavior.CloseConnection)
所以试着把它改成just
Dim rschhWriters As SqlDataReader = chWriters.ExecuteReader()
注意:请务必将一次性物品与 Using - End Using 块一起使用。您的空 Try-Catch 也有问题,因为您隐藏了以后难以调试的问题。您希望查看任何异常,以便更改代码以防止它们发生。
【讨论】:
以上是关于ExecuteReader 需要一个打开且可用的连接 - 当前状态为关闭的主要内容,如果未能解决你的问题,请参考以下文章
System.InvalidOperationException:ExecuteReader 需要打开且可用的连接。连接的当前状态为关闭
ExecuteReader 需要一个开放且可用的连接。连接的当前状态是 Connecting