sql >> Database teknologi >  >> RDS >> Sqlserver

hente data fra SQL i VB (del 2)

Du gik glip af connectionString
Hvis du vil udfylde listen fra DB, er der mange måder

Med 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

Med 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


REDIGERING:
Andre parametre for forbindelsesstrengen afhænger af din sikkerhed og alt det der. Du skal se dette link Forbindelsesstrenge til SQL Server 2008



  1. Sådan får du de nærmeste datoer i Oracle sql

  2. ORA-04076:ugyldig ny eller gammel specifikation - PL/SQL - Oracle Trigger

  3. Forespørger på stor tabel i sql server 2008

  4. Sådan opdateres Identity Column i SQL Server?