ExecuteReader 需要一个开放且可用的连接。 连接当前状态已关闭

发布于 2024-07-19 08:16:39 字数 1070 浏览 7 评论 0原文

好吧,我在本周早些时候询问了这个错误,并得到了一些非常有用的答案,毫无疑问,自从我开始遵循这些建议以来,情况已经有了很大的改善。

但是,现在我使用“正确”的最佳实践方法来访问数据库,我仍然在某些函数上遇到此错误,并且无法让它在该块中消失。 这是我的代码:

    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

<逆流佳人身旁 2024-07-26 08:16:39

我认为您忘记打开连接。

在这一行之前打开它:

cmd.Parameters.AddWithValue("@baskethash", baskethash)

使用 -

db.Open()

I think you have forgotten to open the connection.

Open it before this line:

cmd.Parameters.AddWithValue("@baskethash", baskethash)

Using -

db.Open()
你不是我要的菜∠ 2024-07-26 08:16:39

您实际上忘记了Open 连接:

        db.Open()
        Using dr As SqlDataReader = cmd.ExecuteReader()

You actually forgot to Open connection:

        db.Open()
        Using dr As SqlDataReader = cmd.ExecuteReader()
春庭雪 2024-07-26 08:16:39

原因之一是您的连接根本无法打开。 “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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文