我有一个代码,但是没有用。我试图在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

最佳答案

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

10-07 13:11