SQL ExecuteReader 需要一个打开且可用的连接。连接的当前状态是关闭的

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/839014/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 01:58:52  来源:igfitidea点击:

ExecuteReader requires an open and available Connection. The connection's current state is closed

sqlsqldatareadersqlconnection

提问by dooburt

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?

现在无论我做什么,我都会得到: ExecuteReader 需要一个开放且可用的连接。连接的当前状态是关闭的。在这个连接上。我确实有在这个类中调用相同对象的函数(cmd、dr 等)但是 Using 在它自己之后关闭不是吗?

Suggestions welcome :)

欢迎提出建议:)

回答by Kirtan

I think you have forgotten to open the connection.

我想你忘记打开连接了。

Open it before this line:

在这一行之前打开它:

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

Using -

使用 -

db.Open()

回答by Anton Gogolev

You actually forgot to Openconnection:

您实际上忘记了Open连接:

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

回答by Syed Salman Akbar

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.

原因之一是您的连接根本无法打开。“SqlConnection.Open”语句中出现的任何异常都被抑制了。如果问题不在您的应用程序中,则可能是服务器无法授予您连接。可能是因为您的应用程序或托管在同一服务器上的其他数据库中的连接泄漏。