' System.IndexOutOfRangeException'接着是' System.Reflection.TargetInvocationException'

时间:2015-08-27 01:39:17

标签: mysql vb.net

执行查询时,我总是遇到这两个错误

A first chance exception of type 'System.IndexOutOfRangeException' occurred in MySql.Data.dll

其次是

A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll

这就是我执行查询的方式

Sub Get_Teller_Num(hardware_Add As String)

    Check_DB_Con() 'Check if the connection is okay
    SQL_Query = "SELECT teller_info.teller_num, service_info.service_num " _
        & "FROM service_info " _
        & "JOIN teller_info " _
        & "ON service_info.service_id = teller_info.teller_id " _
        & "WHERE teller_info.hardware_add = " & hardware_Add
    Dim MySQL_CMD As New MySqlCommand(SQL_Query, MysqlConn)

    Try
        MySQL_CMD.Connection.Open()
        MySQL_Reader = MySQL_CMD.ExecuteReader()

        While MySQL_Reader.Read
            teller_Num = MySQL_Reader("teller_info.teller_num")
            service_Num = MySQL_Reader("service_info.service_num")
        End While
        MySQL_Reader.Close()
    Catch myerror As MySqlException
        Console.WriteLine("Failed to run query: " & myerror.Message)
    Finally
        MysqlConn.Close()
        MysqlConn.Dispose()
    End Try
End Sub

正如你所看到的,我正在加入另一张桌子,所以我可以得到一些值。

1 个答案:

答案 0 :(得分:0)

通过在我的ALIAS or AS子句中添加WHEREreading it based on it来解决问题似乎已经解决了。

所以不是这个查询:

SQL_Query = "SELECT teller_info.teller_num, service_info.service_num " _
        & "FROM service_info " _
        & "JOIN teller_info " _
        & "ON service_info.service_id = teller_info.teller_id " _
        & "WHERE teller_info.hardware_add = " & hardware_Add

将其更改为 (Notice that I create an ALIAS for the fields that I want to get)

 SQL_Query = "SELECT teller_info.teller_num AS Teller_Num, service_info.service_num AS Service_Num " _
        & "FROM service_info " _
        & "JOIN teller_info " _
        & "ON service_info.service_id = teller_info.teller_id " _
        & "WHERE teller_info.hardware_add = " & hardware_Add

然后,我们还需要改变读取数据的方式。

而不是:

While MySQL_Reader.Read
    teller_Num = MySQL_Reader("teller_info.teller_num")
    service_Num = MySQL_Reader("service_info.service_num")
End While

将其更改为 (Notice that I read them based on the ALIAS that I've given to them)

While MySQL_Reader.Read
    teller_Num = MySQL_Reader("Teller_Num")
    service_Num = MySQL_Reader("Service_Num")
End While