如何通过vb.net在标签中显示mysql数据库中的选定值

时间:2014-09-16 10:40:33

标签: mysql vb.net

我有一个代码,但它没有用。我试图在label2中设置一个值,但它不起作用。请帮帮我。

Private Sub student_no_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles student_no.Click
    MySqlConnection = New MySqlConnection
    MySqlConnection.ConnectionString = "server = localhost; port=3307; user id = root; password = 1234; database = sample;"
    Dim READER As MySqlDataReader

    Try
        MySqlConnection.Open()
        Dim query As String
        query = " select id from sample.student where last_name = '" & txtlastname.Text & "' "
        Dim Command As New MySqlCommand(query, MySqlConnection)
        READER = Command.ExecuteReader
        Label2.Text = query.ToString

        MessageBox.Show("Student Number Generated")
        MySqlConnection.Close()

    Catch ex As Exception
        MessageBox.Show(ex.Message)
    Finally
        MySqlConnection.Dispose()

    End Try

End Sub

2 个答案:

答案 0 :(得分:1)

您在查询中使用.ToString,这是一个字符串。你应该做的是对READER对象的操作。

由于SELECT将始终返回结果列表,因此您必须按原样处理结果,例如......

While READER.Read()
  MessageBox.Show((READER.GetInt32(0)))
End While

.Read()返回返回的行集中的下一个元素

答案 1 :(得分:0)

If READER.Read() Then
    Label2.Text = READER.GetString(0)
End If