Get Names of Tables in Database

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

How do I, in VB get the names of all the tables in an Access data bas

Also, how do I programatically get all the field names in a specific table

Much appreciated.
 
Hi,

Add a reference to microsoft ado ext 2.8 for dll and security
and microsoft activex data objects 2.8 library in the com tab.

Imports System.Security
Imports System.Security.Permissions
Imports System.Runtime.InteropServices
Imports ADOX

<Assembly: SecurityPermission(SecurityAction.RequestMinimum)>
<ComVisible(False)> _
Module Module1

Sub Main()
Dim tbl As New Table
Dim cnn As New ADODB.Connection
Dim cat As New ADOX.Catalog
Dim cmd As New ADODB.Command

' Open the Connection
cnn.Open( _
"Provider='Microsoft.Jet.OLEDB.4.0';" & _
"Data Source='Northwind.mdb';")

' Open the catalog
cat.ActiveConnection = cnn

For Each tbl In cat.Tables
Dim col As ADOX.Column
For Each col In tbl.Columns
Console.WriteLine(String.Format("Table {0} Column Name {1}",
tbl.Name, col.Name))
Next
Next

cnn.Close()
End Sub

End Module


Ken
 
Thanks for your Help

The statement: Imports ADOX - get error message ADOX cannot b found

Thanks
 
Dim Cnstr As String
Dim dc As DataRow
Dim i As Integer
Dim j As Integer

Cnstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = C:\Program
Files\Microsoft Visual Studio\VB98\NWIND.MDB"

Dim Con As OleDb.OleDbConnection = New OleDb.OleDbConnection(Cnstr)
Con.Open()

Dim DBTables As DataTable =
Con.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, New
Object() {Nothing, Nothing, Nothing, "TABLE"})

For i = 0 To DBTables.Columns.Count - 1

If DBTables.Columns(i).ToString = "TABLE_NAME" Then

For j = 0 To DBTables.Rows.Count - 1

dc = DBTables.Rows(j)

Console.Out.WriteLine(dc.Item(i))

Next

End If

Next i

Dim Da as OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter("Select * From
Products", Con)

Dim Tbl As new DataTable

da.Fill(Tbl)

For i = 0 To Tbl.Columns.Count - 1

Console.Out.WriteLine(tbl.Columns(i).ToString())

Next
 
Have you added the reference? Right click on References in Solution
Explorer, Add Reference. Scroll down the Com list to find Microsoft ADO.
 
Back
Top