ExecuteReader 需要一个打开且可用的连接 - 当前状态已关闭
ExecuteReader requires an open and available Connection - current state is closed
所有。
自从昨晚很晚才提出这个问题以来,我一直在努力解决这个问题。
不确定为什么会发生,但确实如此。过去只使用一个 Artistcon.open()
现在要求我在每个新 "ExecuteReader".
都拥有它
下面是代码示例。从连接字符串开始,然后是实际代码。
我在下面标记了一行,以便于查找。
我试图将 ArtistCon.open() 放在代码区域内,在该区域的 select 语句上方,但它出错了,
web.config => database.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 上,然后我在嵌套语句中得到这个。
谢谢。
您特别要求关闭与此电话的连接:
Dim rschhWriters As SqlDataReader
rschhWriters = chWriters.ExecuteReader(Data.CommandBehavior.CloseConnection)
所以尝试将其更改为
Dim rschhWriters As SqlDataReader = chWriters.ExecuteReader()
注意:请使用 Using - End Using 积木来使用您的一次性物品。您空的 Try-Catch 也有问题,因为您隐藏了以后难以调试的问题。您想查看任何异常,以便您可以更改代码以防止它们发生。
所有。
自从昨晚很晚才提出这个问题以来,我一直在努力解决这个问题。
不确定为什么会发生,但确实如此。过去只使用一个 Artistcon.open()
现在要求我在每个新 "ExecuteReader".
都拥有它
下面是代码示例。从连接字符串开始,然后是实际代码。
我在下面标记了一行,以便于查找。
我试图将 ArtistCon.open() 放在代码区域内,在该区域的 select 语句上方,但它出错了,
web.config => database.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 上,然后我在嵌套语句中得到这个。
谢谢。
您特别要求关闭与此电话的连接:
Dim rschhWriters As SqlDataReader
rschhWriters = chWriters.ExecuteReader(Data.CommandBehavior.CloseConnection)
所以尝试将其更改为
Dim rschhWriters As SqlDataReader = chWriters.ExecuteReader()
注意:请使用 Using - End Using 积木来使用您的一次性物品。您空的 Try-Catch 也有问题,因为您隐藏了以后难以调试的问题。您想查看任何异常,以便您可以更改代码以防止它们发生。