从表VB.net中选择MAX(值)

时间:2015-11-09 17:51:38

标签: mysql vb.net

我的这个表包含多个值,范围从1到3000,但它会一直重新调整false

这里出了什么问题?

Dim connectionString As String = "Server=**; Uid=**; Pwd=**; Database=**"
Using SQLConnection As New MySqlConnection(connectionString)
    Using sqlCommand As New MySqlCommand()
        With sqlCommand
            .CommandText = "SELECT MAX(CAST(points AS UNSIGNED)) FROM score"
            .Connection = SQLConnection
            .CommandType = CommandType.Text

        End With
        Try
            SQLConnection.Open()
            Using reader As MySqlDataReader = sqlCommand.ExecuteReader
                While (reader.Read())
                    label1.Text = reader.Read()
                End While
            End Using

        Catch ex As MySqlException
            MsgBox(ex.Message.ToString)
        Finally
            SQLConnection.Close()
        End Try
    End Using

End Using

2 个答案:

答案 0 :(得分:2)

可能你需要阅读不再调用的字段阅读

label1.Text = reader(0).ToString()

但是,要像您一样读取标量值,最好使用ExecuteScalar方法。它只返回查询的单个值而不创建MySqlDataReader以及当您想要逐个读取多个记录时所需的所有基础结构

Using sqlCommand As New MySqlCommand()
    With sqlCommand
        .CommandText = "SELECT MAX(CAST(points AS UNSIGNED)) FROM score"
        .Connection = SQLConnection
        .CommandType = CommandType.Text

    End With
    Try
        SQLConnection.Open()
        Dim result = Convert.ToInt64(sqlCommand.ExecuteScalar())
        label1.Text = result.ToString()
        .....

答案 1 :(得分:1)

Read()前进到下一行/结果(并返回一个布尔值,表示你是否已经通过了结果集的结尾);您正在寻找GetInt32()或类似方法。