Mysql ';系统索引自动失效异常';其次是';System.Reflection.TargetingException';

Mysql ';系统索引自动失效异常';其次是';System.Reflection.TargetingException';,mysql,vb.net,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

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

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

正如您所看到的,我正在加入另一个表,以便可以向其中获取某些值。

似乎通过在我的
WHERE
子句中添加
别名或As
,并基于它读取
解决了问题

因此,与此查询相反:

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
将其更改为
(注意,我为要获取的字段创建了一个别名)

 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_Num")
    service_Num = MySQL_Reader("Service_Num")
End While
然后,我们还需要改变读取数据的方式

与此相反:

While MySQL_Reader.Read
    teller_Num = MySQL_Reader("teller_info.teller_num")
    service_Num = MySQL_Reader("service_info.service_num")
End While
将其更改为
(注意,我是根据我给他们的别名来阅读的)

 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_Num")
    service_Num = MySQL_Reader("Service_Num")
End While