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

System.InvalidOperationException: ExecuteReader requires an open and available connection. The connection's current state is closed

我在使用不同解决方案的客户服务模块时遇到问题。我认为问题出在我调用或获取连接的方法中

这是我的 class 调用的 DBConnForAccess

Imports System.Data.OleDb
Imports System.Data.Odbc

Public Class DBConnForAccess

Dim Conn As New OleDbConnection
Dim cmd As New OleDbCommand
Dim Trans As OleDbTransaction

Public Function DBConnect(ByVal filePath As String, pass As String)
    Try
        ' open Database

        'Conn = New SqlConnection("Data Source=" + ServerName + "\" + DBName + ";User ID=" + DBUsername + ";Password=" + DBPassword + ";Initial Catalog= '" + TB + "'")
        Conn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Jet OLEDB:Database Password=" + pass + ";")
        '                          "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\mydatabase.mdb;Jet OLEDB:Database Password=MyDbPassword;"

        If Conn.State = ConnectionState.Closed Then
            Conn.Open()
        End If

        ' create transaction
        Trans = Conn.BeginTransaction
        Return "Ok"

    Catch ex As Exception
        Return ex.Message
    End Try

End Function

Public Sub ExecuteSQL(ByVal sql As String, ByVal ParamArray Obj() As Object)
    ' command Object
    Dim CMD As New OleDbCommand(sql, Me.Conn, Me.Trans)

    'add the parameters to the sql command
    Dim I As Integer
    For I = 0 To Obj.Length - 1
        CMD.Parameters.AddWithValue("@" & I, Obj(I))
    Next

    'execute the sql
    CMD.ExecuteNonQuery()
End Sub

Public Sub commit()
    Me.Trans.Commit()
    Me.Trans = Me.Conn.BeginTransaction
End Sub

Public Sub rollback()
    Me.Trans.Rollback()
    Me.Trans = Me.Conn.BeginTransaction
End Sub

Public Sub CloseDB()
    Me.Conn.Close()
    Me.Conn.Dispose()
    Me.Trans.Dispose()
End Sub

Public Function ReadData(ByVal sql As String, ByVal ParamArray Obj() As Object)
    ' command Object
    Dim CMD As New OleDbCommand(sql, Me.Conn, Me.Trans)

    'add the parameters to the sql command
    Dim I As Integer
    For I = 0 To Obj.Length - 1
        CMD.Parameters.AddWithValue("@" & I, Obj(I))
    Next

    Dim R = CMD.ExecuteReader()
    'Do While R.Read

    'Loop
    Return R

End Function
End Class

这是我用来获取数据的 Sub:

Dim connection As String = txt_util.readFromTextFile("ConnStr_2.txt", 0) + "Billing.mdb"
 'connection string is in a text file with text at line 0 as "C:\BILLSERV\"

 Private Sub searchAccnt(ByVal SIN As String)
    'clear other fields
    Clear("Acct")
    Try
        AccntDetail.DBConnect("ConcessionairesAccnt")
        'Dim RS = AccntDetail.ReadData("Select * From AllAccounts Where SIN=@0", txtSIN.Text.Trim)
        Dim RS = AccntDetail.ReadData("Select * From viewCSFAccnt Where SIN=@0", SIN)

        RS.Read()
        If RS.Hasrows = 0 Then
            MsgBox("Accounts not found. Check the SIN you Enter.", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, "Records not found.")
            'txtAppNo.Focus()
            Exit Sub
        Else
            'MsgBox("Accounts correct.", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, "Records found.")
            txtZoneNo.Text = RS.Item("Zone").ToString
            txtSeq.Text = RS.Item("SeqNo").ToString
            txtAccntName.Text = RS.Item("AccountName").ToString
            txtAccntAddress.Text = RS.Item("HouseNo").ToString + " " + RS.Item("BLDGName").ToString + " " + RS.Item("Street").ToString + _
                                   " " + RS.Item("BRGY").ToString
            txtMeterNo.Text = RS.Item("MeterNo").ToString
            txtAccntStatus.Text = varA.AccntStatus(RS.Item("Status").ToString)


            'Dim con = AccessAccnt.DBConnect(connection, "")
            'If con <> "Ok" Then
            '    MsgBox("Cannot establish a Connection to the server.", MsgBoxStyle.Critical, "Connection Lost...")
            'End If

            Dim ZoneNo = "Z" + GetChar(txtZoneNo.Text, 1) + GetChar(txtZoneNo.Text, 2) + "B" + GetChar(txtZoneNo.Text, 3) + GetChar(txtZoneNo.Text, 4)
            AccessAccnt.DBConnect(connection, "")
            Dim Acc = AccessAccnt.ReadData("SELECT * FROM " & ZoneNo & " WHERE AcctNo3=@1", txtSeq.Text)
            Acc.Read()
            txtLastReading.Text = Acc.Item("LastReading").ToString
            Acc.close()
            AccessAccnt.CloseDB()

        End If
        RS.Dispose()
        AccntDetail.CloseDB()

        dbCounter.DBConnect("ConcessionairesAccnt")
        Dim result = dbCounter.ReadData("Select Top 1 CSFNo From CSFMaster WHERE (CSFNo LIKE '%" & ctrDate() & "%') order by CSFNo Desc")
        result.read()
        If result.hasrows = 0 Then
            txtTrackingNo.Text = ctrDate() & "-" & "000001"
        Else
            txtTrackingNo.Text = counter.CtrLastItemWithChar("ConcessionairesAccnt", "CSFNo", "CSFMaster", "WHERE (CSFNo LIKE '%" & ctrDate() & "%') ORDER BY CSFNo DESC", 5)
        End If
        dbCounter.CloseDB()

    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try

End Sub

此处抛出错误:

Dim Acc = AccessAccnt.ReadData("SELECT * FROM " & ZoneNo & " WHERE AcctNo3=@1", txtSeq.Text)

它说:

System.InvalidOperationException: ExecuteReader requires an open and available connection. The connection's current state is closed.

子工作的其他部分很好,我在 MSSQL Server 数据库上获取数据的部分。问题出在访问数据获取代码上。

我尝试在另一个项目上使用代码(只是我获取访问数据的代码)我在其他解决方案上工作。但是我将我的代码复制并粘贴到此解决方案中的任何表单上,但我一直给出错误。

我想也许我在某处关闭了连接,但这是我在整个项目中使用此代码的唯一实例。 (只是为了获取最后的阅读记录。)

数据库位于正确的位置 (C:\BILLSERV)

我已经尝试在 SE 上搜索它,但我只能看到关于可能忘记打开连接的建议。我以前使用过这段代码,这段代码适用于我的其他解决方案。我似乎无法让它在这个特定项目上工作。我想知道为什么..

我尝试 运行使用此代码在另一个项目中运行,它工作正常。

VB.net 2012 上是否存在有关 Access 连接的错误,我已经使用此代码 (DBConnForAccess Class) 大约一年了,这是我第一次遇到此错误.顺便说一句,我使用 Access 2003,因为这个数据库曾经是用于在 VB6 中创建的旧系统。

最后这可能是因为解决方案来自另一台使用与我相同的 VB.Net 程序的计算机。因为我们在这里作为一个团队工作。希望对这些系统有更好了解的人可以提供帮助。提前致谢。

编辑

如果建立了连接,访问中应该会出现一个 .ldb 文件。 (我在另一个项目上对此进行了测试,一旦建立连接,就会出现一个 ldb 文件,因为我们都知道 ldb 文件包含使用 db 文件的用户的数据。)我尝试重新 运行 系统和当我使用系统时,没有创建 ldb 文件。

另外,我想也许我在某处关闭了连接,但 这是我在 access 中打开连接并且我在整个项目中使用了代码的唯一实例。 (只是为了获取最后的阅读记录。)

所以这不是 的副本 只是为了澄清

经过一周的调试和阅读网络文章。 我们找到了错误的罪魁祸首。

似乎目标CPU选项高级编译器设置中被更改为AnuCPU.

我们在检查了我们使用的其他应用程序项目后注意到了这一点。

将其改回 x86 即可解决连接问题。

我想知道为什么这会影响到访问的连接。

现在一切正常。感谢您 Plutonix and Steve 提出的所有建议,我们将对代码进行更改。