Thursday, March 30, 2006

How to: Get list of table names from MS Access Database in ASP.NET 2.0

To get a list of table names from database use following code:

Dim sConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\sample.mdb"
Dim olecon As New OleDbConnection(sConn)
olecon.Open()
Dim tblrestrictions As String() = New String() {Nothing, Nothing, Nothing, "TABLE"}
Dim dt As DataTable = olecon.GetSchema("tables", tblrestrictions)
olecon.Close()
GridView1.DataSource = dt
GridView1.DataBind()

Restrictions are a string array in the following format:
{TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, TABLE_TYPE}.

Using ADOX and Msysobjects works fine with windows applications but may problematic for web apps[permission related problems]. If you use Msysobjects directly in query, following error will be displayed:

Record(s) cannot be read; no read permission on 'MSysObjects'

So it's better to use GetSchema with restriction.

2 comments:

Anonymous said...

This was really helpful for me. Thanx. A great post.

Gayan Munasinghe

Anonymous said...

I tried a lot....
Ur code only help me to reach the answer....
Thank You Sir....
Thanks a Lot.....

Visitor Count: