views:

106

answers:

3

Hi,

I am working with Microsoft visual studio 2005.Can anyone tell me how to get table names and column names of a MS access database?

+1  A: 

Typically, you should the metadata facilities of your database driver to do that. All database call level APIs I have heard of supply methods/functions/procedures that can return database metadata, typically as a resultset.

You didn't mention any language, so lets assume you're using C#. In that case, you'd make a OleDB (OleDB is driver framework for .NET languages) connection object, and invoke the GetOleDbSchemaTable method on it. This will then give you a DataTable object that gives you access to the rows of data that convey this information.

For an OleDB example, see: http://msdn.microsoft.com/en-us/library/aa288452(VS.71).aspx

For the GetOleDbSchemaTable method, see: http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbconnection.getoledbschematable(VS.71).aspx

For information on the types of metadata provided by OleDB, see: http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbschemaguid_members(VS.71).aspx

Roland Bouman
Thanks Ronald for quick reply..i tried this..but here we will be using for loop. i just wanted to know a method without using for loop
sona
Sona, it's "Roland". Never mind that, common mistake. Anyway, I guess I don't understand then. I mean, there are multiple tables, and multiple columns. You have to iterate somehow, anyhow, correct? Please explain how you would process the data without a looping construct.
Roland Bouman
Sorry Roland...you right we need looping construct...but if tables contain so many columns means it will effect perfromance..so i am checking any functionality is there in ADO.net to get column names without using for loop. I found that in SQL we can use SELECT COLUMN_NAME,DATA_TYPE,CHARACTER_MAXIMUM_LENGTH FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '" + Customers + "'""; to get column structure...do you know anything like this in MS access
sona
How is returning a recordset from your SQL statement any different than using the recordset returned by the ADO OpenSchema in @Remou's answer?
David-W-Fenton
Sona, about the information_schema: although defined by the SQL standard this is not supported by MS Access, although some of that functionality can be achieved with the tables suggested by Mustafa Magdy. So this is exactly why I suggested you should use the driver's metadata facility: that offers a reasonable common ground no matter what RDBMS you connect to. And concerning the number of columns: on which information do you base this assumption? How many tables/columns do you expect to be processing?
Roland Bouman
+1  A: 

you can do this by querying the following tables

  • MSysAccessObjects

    MSysAccessXML

    MSysACEs

    MSysObjects

    MSysQueries

    MSysRelationships

if these tables are not shown open tables options and on "View" tab check "System Objects"

Mustafa Magdy
I would advise against that. It is possible that these objects are not available to every user. In addition, this is specific to access, and AFAIK, which of these tables are available, and which columns they have varies with the access version. If you use driver metadata, your code is more or less portable.
Roland Bouman
+1  A: 

This example is VBA, but it should be possible to translate the idea as it uses ADO.

  Dim rs As ADODB.Recordset
  Dim cn As ADODB.Connection

      Set cn = "Provider = Microsoft.Jet.OLEDB.4.0; " _
             & "Data Source = MyDB.mdb"

      Set rs = cn.OpenSchema( _
          adSchemaTables, Array(Empty, Empty, Empty))
      Debug.Print rs.GetString
      Set rs = Nothing

Similarly to the above, adSchemaColumns can be used to return columns. The constraints are TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME, so you can see that column data for a specific table can be returned, or all columns and the associated table name.

Some more information: http://msdn.microsoft.com/en-us/library/kcax58fh%28VS.80%29.aspx

Remou