【问题标题】:Query not returning all records, need all records查询不返回所有记录,需要所有记录
【发布时间】:2013-03-21 05:58:53
【问题描述】:

我已经编写了一些代码来从我的数据库中检索 3 个单独的列,但由于某种原因,它没有加载我的查询产生的所有记录。
或者好吧,至少它似乎没有这样做。

此外,由于某种原因,它不会向我显示一个消息框,该消息框应该告诉我在阅读器关闭后已阅读了多少条记录。

这是我的代码:

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

编辑:我注意到当连续两次调用表单时,我第二次收到此错误:

Microsoft.VisualBasic.dll 中出现“System.ArgumentException”类型的未处理异常 附加信息:参数“长度”必须大于或等于零。

它指的是这行代码:

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

最后但同样重要的是,该行代码中使用的值:

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

PS:这可能会有所帮助:survivor.worldspace 中的值最初采用以下格式:

[168,[1291.16,5343.54,0.27]]

【问题讨论】:

    标签: mysql vb.net mysqldatareader


    【解决方案1】:

    如果没有显示消息框,那么最可能的情况是在 while 循环中抛出了一个异常,该异常可能在其他地方被静默捕获。

    不幸的是,有太多地方可能会在该 while 循环中发生异常,因此仅查看该代码很难说。它可能试图将DBNull 转换为字符串,或者索引越界等。

    我的建议是使用调试器单步执行并识别有问题的行,或者在 while 循环中放置一个 try catch 并在 catch 中放置一个断点。这应该为您提供有关异常发生的原因(和位置)的信息。

    根据您的更新,问题似乎在于传递给 Mid() 函数的参数。根据您的数据,您似乎正在尝试使用 1 的开始索引和 -1 的结束索引来获取 loc 的子字符串,这是 loc.IndexOf(",") 在这种情况下返回的因为loc 中没有,(逗号)。

    您可能想要稍微重构该代码。特别是看起来您实际上是在尝试用, 替换.,但是在您尝试调用Mid() 之后才这样做。这似乎是你的问题!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-09-11
      • 1970-01-01
      • 1970-01-01
      • 2019-05-26
      • 2014-06-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多