2013-03-20 204 views
0

我已經寫了一些代碼來從我的數據庫中檢索3個獨立的列,但由於某種原因,它沒有加載我的查詢產生的所有記錄。或者,至少它似乎並不這樣做。查詢不返回所有記錄,需要所有記錄

另外,出於某種原因,它不會告訴我一個消息箱,它應該告訴我在閱讀器關閉後如何閱讀howmany記錄。

這裏是我的代碼:

Public Class frmPlayerLocations 
    Dim str(2), loc As String 
    Dim xx, yy As Integer 
    Dim itm As ListViewItem 
    Private Sub frmPlayerLocations_Load(sender As Object, e As EventArgs) Handles MyBase.Load 
     ListView1.Columns.Add("ID", 60, HorizontalAlignment.Left) 
     ListView1.Columns.Add("Name", 115, HorizontalAlignment.Left) 
     ListView1.Columns.Add("Approximate Location", 115, HorizontalAlignment.Left) 

     Dim qry = "SELECT profile.unique_id, profile.name, survivor.worldspace FROM profile, survivor WHERE survivor.unique_id = profile.unique_id AND survivor.is_dead = '0' ORDER BY profile.name" 
     Dim connection As MySqlConnection 
     connection = New MySqlConnection() 
     connection.ConnectionString = "Host=" & adminPanel.IP & ";port=" & adminPanel.port & ";user=" & adminPanel.username & ";password=" & adminPanel.password & ";database=" & adminPanel.DBname & ";" 
     connection.Open() 
     Dim cmd As New MySqlCommand(qry, connection) 
     Dim reader As MySqlDataReader = cmd.ExecuteReader() 
     Dim count As Integer = 0 
     While reader.Read() 
      count += 1 
      str(0) = reader.GetString(0) 
      str(1) = reader.GetString(1) 
      loc = reader.GetString(2) 
      loc = Mid(loc, loc.IndexOf(",") + 3) 
      xx = CInt(Replace(Mid(loc, 1, loc.IndexOf(",")), ".", ",", 1, -1, CompareMethod.Text)) 
      xx = (xx/10000) 
      loc = Mid(loc, loc.IndexOf(",") + 2) 
      yy = CInt(Replace(Mid(loc, 1, loc.IndexOf(",")), ".", ",", 1, -1, CompareMethod.Text)) 
      yy = 152 - (yy/10000) 
      If xx < 100 Then 
       If xx < 10 Then 
        loc = "00" & xx.ToString & " | " 
       Else 
        loc = "0" & xx.ToString & " | " 
       End If 
      Else : loc = xx.ToString & " | " 
      End If 
      If yy < 100 Then 
       If yy < 10 Then 
        loc &= "00" & yy.ToString 
       Else 
        loc &= "0" & yy.ToString 
       End If 
      Else : loc &= yy.ToString 
      End If 
      str(2) = loc 
      itm = New ListViewItem(str) 
      ListView1.Items.Add(itm) 
     End While 
     reader.Close() 
     connection.Close() 
     MessageBox.Show(count) 

    End Sub 
End Class 

編輯:我注意到,在一排調用表單時的兩倍,第二次我得到這個錯誤:

An unhandled exception of type 'System.ArgumentException' occurred in Microsoft.VisualBasic.dll Additional information: Argument 'Length' must be greater or equal to zero.

它指的是這個行代碼:

yy = CInt(Replace(Mid(loc, 1, loc.IndexOf(",")), ".", ",", 1, -1, CompareMethod.Text))

最後但並非最不重要的,VALU ES其在該行的代碼中使用:

loc "7.305e-04]]" String 
    yy 131 Integer 

PS:這可能是有益的:它們是在survivor.worldspace的值是在該格式最初:

[168,[1291.16,5343.54,0.27]]

回答

1

如果消息框是沒有被顯示,那麼最可能的情況是在while循環內引發了一個異常,這個異常可能是在別的地方默默地捕獲到的。

不幸的是,只有太多的地方可能會發生異常,因爲只是查看該代碼很難說。它可能試圖將DBNull轉換爲字符串或索引超出範圍等。

我的建議是或者逐個調試器並確定違規行,或者在while循環中放入try catch,在捕獲物內放置一個斷點。這應該給你關於什麼(和在哪裏)的例外情況發生的信息是..

根據您的更新,它看起來像問題是傳遞給Mid()函數的參數。根據您的數據,看起來您嘗試使用起始索引1和結束索引-1獲取子字符串loc,這是loc.IndexOf(",")在這種情況下返回的結果,因爲在該字段中沒有,(逗號) loc

你可能想重新因子的代碼位。尤其它看起來像你實際上是試圖取代.,但你試圖調用Mid()後這樣做。這似乎是你的問題!