ExecuteReader 需要一个打开且可用的连接.连接的当前状态是关闭的 [英] ExecuteReader requires an open and available Connection. The connection's current state is closed

查看:42
本文介绍了ExecuteReader 需要一个打开且可用的连接.连接的当前状态是关闭的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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?

欢迎提出建议:)

推荐答案

我想你忘记打开连接了.

I think you have forgotten to open the connection.

在这一行之前打开它:

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

使用 -

db.Open()

这篇关于ExecuteReader 需要一个打开且可用的连接.连接的当前状态是关闭的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆