Imports System.Data.SqlClient

Public Class List

    Private Sub cmdClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClear.Click
        lvOrders.Clear()
    End Sub

    Private Sub cmdFill_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdFill.Click

        Dim Connect As String = "Data Source=localhost;Integrated Security=True;" & _
          "Initial Catalog=Northwind"
        Dim con As New SqlConnection(Connect)

        Dim SQLString As String = "SELECT * FROM Orders " & _
         "WHERE OrderDate < '2000/01/01' AND OrderDate > '1987/01/01'"
        Dim cmd As New SqlCommand(SQLString, con)

        con.Open()

        Dim reader As SqlDataReader = cmd.ExecuteReader()
        Dim i As Integer
        For i = 0 To reader.FieldCount - 1
            lvOrders.Columns.Add("Kolumna " & (i + 1).ToString, 100, _
             HorizontalAlignment.Left)
        Next

        Do While (reader.Read())
            Dim NewItem As New ListViewItem()
            NewItem.Text = reader(0)

            For i = 1 To reader.FieldCount - 1
                If reader(i) Is DBNull.Value Then
                    NewItem.SubItems.Add("")
                Else
                    NewItem.SubItems.Add(reader(i).ToString)
                End If
            Next i

            lvOrders.Items.Add(NewItem)
        Loop

        ' Dodawanie nazw pól.
        Dim Table As DataTable = reader.GetSchemaTable()
        For j As Integer = 0 To Table.Rows.Count - 1
            lvOrders.Columns(j).Text = Table.Rows(j)("ColumnName")
        Next

        reader.Close()
        con.Close()

    End Sub

End Class