vb.net 如何通过vb.net在标签中显示mysql数据库中的选定值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25866622/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to display selected value in mysql database in label through vb.net
提问by Kaye Santos
i have a code but it's not working. i was trying to put a value in label2 but it's not working. please help me.
我有一个代码,但它不起作用。我试图在 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
采纳答案by dovid
If READER.Read() Then
Label2.Text = READER.GetString(0)
End If
回答by Jesper Dalberg
You are using .ToString on query, which is a string. What you should be doing is operations on the READER object.
您在查询中使用 .ToString,它是一个字符串。您应该做的是对 READER 对象的操作。
Since SELECT will always return a list of results, you have to treat the results as such, like...
由于 SELECT 将始终返回结果列表,因此您必须如此对待结果,例如...
While READER.Read()
MessageBox.Show((READER.GetInt32(0)))
End While
.Read() returns the next element in the returned rowset
.Read() 返回返回的行集中的下一个元素

