2015-08-27 97 views
-1

我一直執行查詢「System.IndexOutOfRangeException」接着「System.Reflection.TargetInvocationException」

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 

當得到這個2個錯誤這是我執行我的查詢

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 

正如您所看到的,我加入了另一個表格,因此我可以獲得某些值。

回答

0

看來,在我的WHERE子句中添加了ALIAS or AS,並且reading 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 
相關問題