在VB.Net中从MySQL中检索数据

时间:2015-05-14 23:22:45

标签: mysql vb.net

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。

1 个答案:

答案 0 :(得分:0)

尝试更改此行

ListBox.Text = sqlReader("Name").ToString()

ListBox.Items.Add(sqlReader("Name").ToString())