3

SQL を使用してデータベースからデータを取得して、リストボックスにデータを入力しようとしています。以前にこの質問をしましたが、別の構成を使用していて、現在使用している構成では結果が得られません。

SQL から VB のデータを取得する

それは私の古い投稿です。ここで、私の試みの新しいバージョンにコードを提供します。

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 はテーブル内の項目の数です。実際の値を取得するにはどうすればよいですか?

4

3 に答える 3

4

DBconnectionString
からリストを作成したい場合は、多くの方法があります

DataReader を使用

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 connectionString As String = "Data Sourec=localhost;........."
    Dim conn As New SqlConnection(connectionString)
    conn.Open()
    Dim comm As New SqlCommand("SELECT name FROM Table_1", conn)
    Dim reader As SqlDataReader = comm.ExecuteReader
    /* As it is not working i commented this
    listBox1.ItemsSource = dt; // use this instead of  ListBox1.Items.Add(dt)
    //because Add event add only one item in the list. 
     */
    Dim i As Integer
    i=0
    while reader.read() 
    listbox1.Items.Add(dr(i).ToString);
    i++
    End While

 End Sub
End Class

DataTable を使用

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 connectionString As String = "Data Sourec=localhost;........."
    Dim conn As New SqlConnection(connectionString)
    conn.Open()
    // Create new DataAdapter
    SqlDataAdapter a = new SqlDataAdapter("SELECT * FROM EmployeeIDs", c)
    // Use DataAdapter to fill DataTable
    DataTable dt = new DataTable();
    a.Fill(dt);
    ListBox1.DataSource = dt;
    ListBox1.DataTextField = "name";



 End Sub
End Class


編集:
接続文字列の他のパラメーターは、セキュリティとそのすべてに依存します。このリンクが必要です SQL Server 2008 の接続文字列

于 2013-02-17T04:03:29.160 に答える
3

バインドDisplayMember後にプロパティを設定します。DataSource

ListBox1.DataSource = dt
ListBox1.DisplayMember="name"
于 2013-02-17T04:47:13.217 に答える
0

The last solution I saw should work, but there are a couple important best practices to keep in mind regarding SQL Server.

1) Avoid Select * whenever possible, instead name your columns explicitly. Select * causes SQL to perform extra work if you do not intend to pull down all the columns in the table. It's also not future-proof, since a dba could add a VARBINARY(MAX) column in the future, and populate it with gigs worth of blob data, This scenario would make your query as written slow down substantially and unnecessarily.

2) Always remember to close your SQLConnection when you're done with it. This will free up a SQL connection and resources.

if (cn.State != ConnectionState.Closed)
cn.Close();

Another cool trick is to use the USING directive which will dispose of the SqlConnection object when execution passes out of scope.

using (SqlConnection cn = new SqlConnection(sConnectionString))
{
    if (cn.State != ConnectionState.Open)
    cn.Open();

   // add query code here.

    if (cn.State != ConnectionState.Closed)
    cn.Close();
}
  1. don't forget to close your SqlDataReader after the read loop is complete.

    if (!dr.IsClosed) dr.Close();

I hope this helps.

Andre Ranieri

于 2013-02-17T19:10:45.823 に答える