SQL を使用してデータベースからデータを取得して、リストボックスにデータを入力しようとしています。以前にこの質問をしましたが、別の構成を使用していて、現在使用している構成では結果が得られません。
それは私の古い投稿です。ここで、私の試みの新しいバージョンにコードを提供します。
Imports System.Data.Sql
Imports System.Data.SqlClient
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim conn As New SqlConnection
conn.Open()
Dim comm As New SqlCommand("SELECT name FROM Table_1", conn)
Dim reader As SqlDataReader = comm.ExecuteReader
Dim dt As New DataTable
dt.Load(reader)
ListBox1.Items.Add(dt)
End Sub
End Class
誰かが私を助けてくれるなら、私はそれを大いに感謝します. 可能であれば、私を啓発しようとするときは実用的なアプローチを使用してください。それが最も効果的です。
編集 1
Imports System.Data.Sql
Imports System.Data.SqlClient
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim connString As String = "Data Source=THE_SHOGUNATE\SQLEXPRESS;Initial Catalog=le_database;Integrated Security=True"
Dim conn As New SqlConnection(connString)
conn.Open()
Dim comm As New SqlCommand("SELECT name FROM Table_1", conn)
Dim reader As SqlDataReader = comm.ExecuteReader
Dim dt As New DataTable
dt.Load(reader)
ListBox1.DataSource = dt
End Sub
End Class
このコードを使用すると、リストボックスに「System.Data.DataRowView」文字列の 6 つのインスタンスが表示されます。6 はテーブル内の項目の数です。実際の値を取得するにはどうすればよいですか?