ExecuteReader需要打开连接。连接的当前状态已关闭

时间:2018-06-10 20:06:38

标签: vb.net

我收到错误:

  

ExecuteReader需要打开连接。连接的当前状态已关闭。

代码是:

 Public Shared Sub FillInvoiceGrid(datagrid As DataGridView)
        Dim connection As OleDbConnection = AutoBeautyCareDB.GetConnection
        Dim selectStatement As String = "SELECT INV_NO,INV_DATE,(C_NAME+' '+C_SURNAME) AS NAME,TOTAL " &
                                        "FROM Invoice INNER JOIN Customer ON Invoice.[CUST#] = Customer.[CUST#]"
        Dim selectCommand As OleDbCommand = New OleDbCommand(selectStatement, connection)
        Try
            connection.Open()
            Dim reader As OleDbDataReader = selectCommand.ExecuteReader(behavior:=CommandBehavior.CloseConnection)
            Dim tb As New DataTable
            tb.Columns.Add("InvoiceNo")
            tb.Columns.Add("InvDate")
            tb.Columns.Add("Customer")
            tb.Columns.Add("Total")
            Do While reader.Read
                tb.Rows.Add(reader("INV_NO"), reader("INV_DATE"), reader("NAME"), reader("TOTAL"))
            Loop
            datagrid.DataSource = tb

        Catch ex As Exception
            Throw ex

        Finally
            connection.Close()

        End Try

    End Sub

0 个答案:

没有答案