Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / June 2006

Tip: Looking for answers? Try searching our database.

Database Metadata // OleDB

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
sloan - 29 Jun 2006 18:53 GMT
Back in ADO days, it was fairly easy to get the meta data (tablenames,
columnnames , etc) .. with all that schema stuff.

I have a Access database, that I'd like to get the list of tableNames in it.

How does one get the metadata about a database thru DotNet?

Thanks.
Steve Barnett - 30 Jun 2006 08:41 GMT
Dloan
   This was part of my first ever .Net app, so it's probably not the best
or most optimised code, but the following returns you a list of tables...
DBDef is a strongly typed dataset, but I'm sure you can get round that. The
important call is the call to "dBase.GetOleDbSchemaTable"  which is where
the table list comes from.

 public DBDef ProcessDatabase()
 {
      // Datatable to receive the list of tables names from the database
      DataTable schemaTable= new DataTable("TableNames");
      DBDef.TableNamesRow newTableName;

  // Setup a connection to the database
           string strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=" + databaseFilename + ";Jet                     OLEDB:Engine
Type=4;";
  OleDbConnection dBase = new OleDbConnection(strConnection);

  // Start by getting a list of tables in to the data table/dataset
  try
  {
       // Open the connection to the database
       dBase.Open();

   // Retrieve the list of data tables in the database, excluding system
tables
   schemaTable = dBase.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new
Object[] {null, null, null,
                    "TABLE"});
   for (int i=0; i < schemaTable.Rows.Count; i++)
   {
       string tableName = schemaTable.Rows[i].ItemArray[2].ToString();

        // For each table in the database, retrieve the column information
        ProcessTable(dBase, tableName);

        newTableName = dbStruct.TableNames.NewTableNamesRow();
        newTableName.dbTableName =
schemaTable.Rows[i].ItemArray[2].ToString();
        dbStruct.TableNames.AddTableNamesRow(newTableName);

        ProcessIndexes(dBase, tableName);
   }
  }
  finally
  {
   dBase.Close();
  }

  return dbStruct;
 }

To get information on specific columns ina table, I use code like this:

   // Create the datareader, retrieving only the schema information
   dataReader = cmd.ExecuteReader(CommandBehavior.KeyInfo);
   // Copy the stuff we retrieved in to a data table
   schemaTable = dataReader.GetSchemaTable();
   // And give the data table a name that we can work with.
   schemaTable.TableName = "ColumnNames";
   // We don't need the data reader any more - we have what we want
   dataReader.Close();

Where "cmd" is an OleDbCommand object with a simple "SELECT * FROM
tablename" statement in it. Again, the main work is done with the
GetSchemaTable call.

HTH
Steve

> Back in ADO days, it was fairly easy to get the meta data (tablenames,
> columnnames , etc) .. with all that schema stuff.
[quoted text clipped - 5 lines]
>
> Thanks.
sloan - 30 Jun 2006 15:02 GMT
Steve,

Thanks.... I appreciate it.

That'll definately save me alot of time.

> Dloan
>     This was part of my first ever .Net app, so it's probably not the best
[quoted text clipped - 76 lines]
> >
> > Thanks.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.