ExecuteReader 需要一个开放且可用的连接。 连接当前状态已关闭
好吧,我在本周早些时候询问了这个错误,并得到了一些非常有用的答案,毫无疑问,自从我开始遵循这些建议以来,情况已经有了很大的改善。
但是,现在我使用“正确”的最佳实践方法来访问数据库,我仍然在某些函数上遇到此错误,并且无法让它在该块中消失。 这是我的代码:
Public Shared Function doesBasketExist(ByVal baskethash As String) As Boolean
Dim _r As Boolean
Using db As New SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("pitstopConnectionString").ConnectionString)
Using cmd As New SqlCommand("doGetBasketByHash", db)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.AddWithValue("@baskethash", baskethash)
Using dr As SqlDataReader = cmd.ExecuteReader()
If dr.HasRows() = True Then
_r = True
Else
_r = False
End If
dr.Close()
End Using
End Using
End Using
Return _r
End Function
现在,无论我做什么,我都会得到: ExecuteReader 需要一个开放且可用的连接。 连接的当前状态已关闭。 在这个连接上。 我确实有一些函数,其中的对象在此类中称为相同的东西(cmd、dr 等),但使用会在其自身之后关闭,不是吗?
欢迎提出建议:)
Ok, I asked about this very error earlier this week and had some very helpful answers and without doubt things have drastically improved since I started following the suggestions.
However, now I am using the 'correct', best practice method to access the database I still get this error on some functions and I cannot get it to disappear for that block. Here is my code:
Public Shared Function doesBasketExist(ByVal baskethash As String) As Boolean
Dim _r As Boolean
Using db As New SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("pitstopConnectionString").ConnectionString)
Using cmd As New SqlCommand("doGetBasketByHash", db)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.AddWithValue("@baskethash", baskethash)
Using dr As SqlDataReader = cmd.ExecuteReader()
If dr.HasRows() = True Then
_r = True
Else
_r = False
End If
dr.Close()
End Using
End Using
End Using
Return _r
End Function
Now no matter what I do I get: ExecuteReader requires an open and available Connection. The connection's current state is closed. on this connection. I do have functions with objects called the same thing within this class (cmd, dr etc.) but Using closes up after itself doesn't it?
Suggestions welcome :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为您忘记打开连接。
在这一行之前打开它:
使用 -
I think you have forgotten to open the connection.
Open it before this line:
Using -
您实际上忘记了
Open
连接:You actually forgot to
Open
connection:原因之一是您的连接根本无法打开。 “SqlConnection.Open”语句中出现的任何异常都将被抑制。 如果问题不在您的应用程序中,则可能是服务器无法授予您连接。 可能是由于您的应用程序或同一服务器上托管的某些其他数据库中的连接泄漏。
One reason for this would be that your connection could not be opening at all. What ever exception that is coming at the "SqlConnection.Open" statement is being suppressed. If the problem is not in your application it could be that server is unable to grant you a connection. Could be because of a connection leak in your app or in some other database hosted on the same server.