0

I want to show the names of the tables I have in my SQL Server database in a listbox using vb.net.

I tried this but it's not working:

 connetionString = "Data Source=ABDELOUAHED;Initial Catalog=table_creances;integrated security=true"
 connection = New SqlConnection(connetionString)

 sql = "select * from table_creances"

 Try
        connection.Open()
        adapter = New SqlDataAdapter(sql, connection)
        ds.Clear()
        adapter.Fill(ds)
        connection.Close()
        ListBox1.DataSource = ds.Tables(0)
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try

table_creances is the name of my database.

Any help is very appreciated.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

2 Answers2

1

Try use system view sys.objects

try this code:

Using connection As New SqlConnection(connetionString)
    connection.Open()
    adapter = New SqlDataAdapter("SELECT name FROM sys.tables", connection)
    Using ds = new DataSet()
        adapter.Fill(ds)
        with ListBox1
            .DataSource = ds.Tables(0)
            .DisplayMember = "name"
            .ValueMember = "name"
        end with
    End Using
End Using
Adam Silenko
  • 3,025
  • 1
  • 14
  • 30
0

Try this as your sql

sql = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES"
Tonny
  • 109
  • 9