473,385 Members | 1,907 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Database Metadata // OleDB


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.

Jun 29 '06 #1
2 6483
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
"sloan" <sl***@ipass.net> wrote in message
news:uM**************@TK2MSFTNGP04.phx.gbl...

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.

Jun 30 '06 #2


Steve,

Thanks.... I appreciate it.

That'll definately save me alot of time.


"Steve Barnett" <no****@nodomain.com> wrote in message
news:uH**************@TK2MSFTNGP03.phx.gbl...
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
"sloan" <sl***@ipass.net> wrote in message
news:uM**************@TK2MSFTNGP04.phx.gbl...

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.


Jun 30 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Pam Ammond | last post by:
I need the code to update the database when Save is clicked and a text field has changed. This should be very easy since I used Microsoft's wizards for the OleDBAdapter and OleDBConnection, and...
3
by: Brian Foree | last post by:
I am developing an ASP.NET application that uses Access 2000 as its backend, and have just started getting the following error on 2 ASP.NET pages that had been working until late last week (and I...
18
by: Wim | last post by:
Hi, I would like to make and put a query in my database by VB.NET code. Is this possible? Thanks for your help, wim
5
by: plenahan68 | last post by:
I've made a small vb.net program to read my database and delete records that are specific to my needs. Well, the program says it's complete but nothing is deleted from my database. I've listed the...
34
by: Karam Chand | last post by:
Hello I have been working with Access and MySQL for pretty long time. Very simple and able to perform their jobs. I dont need to start a flame anymore :) I have to work with PGSQL for my...
10
by: jt | last post by:
The program works like this: There is a form with a button. When the form is loaded, a separate thread is started which is retreiving/updating data in the database every x seconds. When clicked...
5
by: Chris Brat | last post by:
Hi, Is it possible to retrieve details about the database, specifically a list of the tables in the database; and then to retrieve the columns and their types for the tables? Is this...
0
by: DeepthiSem | last post by:
Hi Everyone I have an issue while saving .rpt file into DB2 database using C# windows application. The provider i have used is OLEDB provider. It is not supporting to insert large amount of data...
1
by: thomasma57 | last post by:
How do you connect an ADO control to a MS Access database that has a password. I am using Jet.OLEBD.
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.