Home > Software engineering >  Displaying Table names in a listbox (VB)
Displaying Table names in a listbox (VB)

Time:10-05

Hi i would like to know the syntax on how can I display the table names in a listbox?

I'm using visual studio (vb) with LocalDB and trying to create a program using windows forms that can create a table in my database and displaying the names of the tables in a listbox(or anything similar to a listbox)

CodePudding user response:

This will depend on what version of Sql Server you are using. This should work with 2005 and newer.

Private ConLocal As String = "Your connection string"

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim sql = "Select TABLE_NAME
                FROM INFORMATION_SCHEMA.TABLES
                WHERE TABLE_TYPE = 'BASE TABLE';"
    Dim dt As New DataTable
    Using cn As New SqlConnection(ConLocal),
            cmd As New SqlCommand(sql, cn)
        cn.Open()
        Using reader = cmd.ExecuteReader
            dt.Load(reader)
        End Using
    End Using
    For Each row As DataRow In dt.Rows
        ListBox1.Items.Add(row(0))
    Next
End Sub
  • Related