TGolfersからのすべてのゴルファーの情報をListBoxに表示しようとしています。しかし、自分のコードを走らせると、ゴルファーの情報が1つしか表示されません。SQLからvb.netへのデータ表示リストボックス
Public Class frmGolfers
Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click
Dim strSelect As String = ""
Dim strName As String = ""
Dim cmdSelect As OleDb.OleDbCommand ' this will be used for our Select statement
Dim drSourceTable As OleDb.OleDbDataReader ' this will be where our data is retrieved to
Dim dt As DataTable = New DataTable ' this is the table we will load from our reader
' open the database
If OpenDatabaseConnectionSQLServer() = False Then
' No, warn the user ...
MessageBox.Show(Me, "Database connection error." & vbNewLine & _
"The application will now close.",
Me.Text + " Error",
MessageBoxButtons.OK, MessageBoxIcon.Error)
' and close the form/application
Me.Close()
End If
' Build the select statement using PK from name selected
strSelect = "SELECT * FROM TGolfers "
' Retrieve all the records
cmdSelect = New OleDb.OleDbCommand(strSelect, m_conAdministrator)
drSourceTable = cmdSelect.ExecuteReader
' load the data table from the reader
dt.Load(drSourceTable)
' populate the text boxes with the data
lbxDisplay.Items.Add(dt.Rows(0).Item(1).ToString & "," & " " & dt.Rows(0).Item(2).ToString &
ControlChars.CrLf & " " & dt.Rows(0).Item(3).ToString & " " & dt.Rows(0).Item(4).ToString & " " & dt.Rows(0).Item(5).ToString & " " & dt.Rows(0).Item(6).ToString)
' close the database connection
CloseDatabaseConnection()
End Sub
End Class
リストボックスの項目に追加するためDataTableの最初の行(行(0))で、完全なデータテーブルではありません。あなたはループを書く必要があります(foreachのためのIE |たくさんの選択肢があります) – Steve
お返事ありがとうございます! –
これは私が使ったループです。このエラーは "位置0に行がありません" Dim count As Integer count = 0 drSourceTable.Read中に count = count + 1 終了中 –