|
|
Title | Load a ListBox from a database query in VB .NET |
Keywords | database, VB.NET, ListBox, load |
Categories | VB.NET, Database, Controls |
|
|
Execute the query. Use a data reader object to loop through the results, adding the fields' values to the ListBox separated by tab characters.
|
|
Private Sub LoadListBoxFromQuery(ByVal lst As ListBox, _
ByVal query As String)
lst.Items.Clear()
' Open the connection.
connUsers.Open()
' Make a SELECT Command.
Dim cmd As New OleDb.OleDbCommand( _
query, connUsers)
' Execute the query.
Dim db_reader As OleDbDataReader = _
cmd.ExecuteReader(CommandBehavior.Default)
' Display the results.
Dim txt As String
Dim i As Integer
Do While db_reader.Read
txt = db_reader.Item(0).ToString
For i = 1 To db_reader.FieldCount - 1
txt &= vbTab & db_reader.Item(i).ToString
Next i
lst.Items.Add(txt)
Loop
' Close the connection.
connUsers.Close()
End Sub
|
|
For lots more information on database programming in VB .NET, see my book Visual Basic .NET Database Programming.
|
|
|
|
|
|