访问OleDbDataReader项时出现IndexOutOfRange错误

时间:2014-01-24 12:11:46

标签: vb.net visual-studio-2010 visual-studio ms-access

我有一些在Access 2010中工作正常的sql并返回正确的值。但是,在VB.NET中,当我运行它时会导致while循环中出现上述错误。 dr.HasRows返回' TRUE ',但var' itm '在自动窗口中没有显示任何内容。有人可以解释为什么会发生这种情况以及我如何纠正它。感谢

Dim cmd As OleDbCommand = New OleDbCommand("SELECT Max(Requests.[Request no]) AS [MaxOfRequest no], Requests.Customer, " &
                    "Max([Request Boxes].[Request no]) AS [MaxOfRequest no1], [Request Boxes].Customer " &
                    "FROM Requests, [Request Boxes] " &
                    "GROUP BY Requests.Customer, [Request Boxes].Customer " &
                    "HAVING (((Requests.Customer)='" & cmbCustomer.Text & "') AND (([Request Boxes].Customer)='" & cmbCustomer.Text & "')) " &
                    "ORDER BY Requests.Customer, [Request Boxes].Customer", oledbCnn)

            dr = cmd.ExecuteReader()

            If dr.HasRows Then <--- this returns true

                While dr.Read

                    itm = CStr(dr.Item("[MaxOfRequest no]")) <--- ERROR HERE
                    itm2 = CStr(dr.Item("[MaxOfRequest no1]"))

                End While

            End If

1 个答案:

答案 0 :(得分:2)

这里不需要方括号:

itm = CStr(dr.Item("[MaxOfRequest no]")) 

方括号只是SQL语句中的分隔符;它们不是列名本身的一部分。试试这个:

itm = CStr(dr.Item("MaxOfRequest no"))