2013-03-20 164 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()后这样做。这似乎是你的问题!