Private Sub ListBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox.SelectedIndexChanged
Dim connect As String = "server=localhost;user=root;password=Password;database=giordydatabase"
Dim sqlQuery As String = "SELECT firstName, lastName FROM students WHERE name =@firstName"
Using sqlConn As New MySqlConnection(connect)
Using sqlComm As New MySqlCommand()
With sqlComm
.Connection = sqlConn
.CommandText = sqlQuery
.CommandType = CommandType.Text
.Parameters.AddWithValue("@firstName")
End With
Try
sqlConn.Open()
Dim sqlReader As MySqlDataReader = sqlComm.ExecuteReader()
While sqlReader.Read()
ListBox.Text = sqlReader("Name").ToString()
End While
Catch ex As MySqlException
' add your exception here '
End Try
End Using
End Using
到目前为止,我想知道如何检索数据并将其放入ListBox。
答案 0 :(得分:0)
尝试更改此行
ListBox.Text = sqlReader("Name").ToString()
到
ListBox.Items.Add(sqlReader("Name").ToString())