Retrieving metadata

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

Guest

Hi;

Is there a way to get the metadata for the tables and columns in a database?
What I really want is the description of each table and column (yes I know
that is optional information).

I need to be able to get it from OleDB and Oracle as well as SqlServer.
 
Hi David,

The only common method is OleDbConnection.GetoleDbSchemaTable.
Other then that you'll have to use database specific queries.
 
Hi;

I was looking at that but I didn't see anything that returned the
description of tables and/or columns. Do you know if that info is in there
and if so how to get it?

--
thanks - dave


Miha Markic said:
Hi David,

The only common method is OleDbConnection.GetoleDbSchemaTable.
Other then that you'll have to use database specific queries.

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/
SLODUG - Slovene Developer Users Group www.codezone-si.info

David Thielen said:
Hi;

Is there a way to get the metadata for the tables and columns in a
database?
What I really want is the description of each table and column (yes I know
that is optional information).

I need to be able to get it from OleDB and Oracle as well as SqlServer.
 
Hi David,

As Miha has mentioned, though the OleDbConnection provide the
GetOleDbSchemaTable method, for different underlying db, whether it can
retrieve the proper schema info also depend on the support of the
underlying provider(of the certain database). If the unerlying provider of
that certain db doesn't support this, we may need to use database specific
query as Miha said. Here are some tech articles discussing on retrieving
Database table schema info:

#Retrieving Schema Information Using ADO.NET and C#
http://www.c-sharpcorner.com/Code/2004/July/OledbSchema.asp

#Getting database schema using ADO.NET
http://searchvb.techtarget.com/vsnetTip/1,293823,sid8_gci885470_tax293036,00
..html

Hope helps. Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
David,

Maybe does this snippet help you.

\\\\\
dt = New DataTable
Dim dtschema As DataTable
dtschema = rdr.GetSchemaTable
For Each drschema As DataRow In dtschema.Rows
dt.Columns.Add(drschema("ColumnName").ToString, _
Type.GetType(drschema("DataType").ToString))
Next
///

I hope this helps,

Cor
 
David said:
Hi;

Is there a way to get the metadata for the tables and columns in a
database? What I really want is the description of each table and
column (yes I know that is optional information).

I need to be able to get it from OleDB and Oracle as well as
SqlServer.

OleDB has a method for that (see other posts) though it's very buggy:
it doesn't always give back results which are correct.

I pressume you want the 'Extended properties' for sqlserver and the
'Description' elements on oracle?

SqlServer:
SELECT sysobjects.Name AS ObjectName,
sysobjects.xtype AS ObjectType,
user_name(sysobjects.uid) AS SchemaOwner,
sysproperties.name AS PropertyName,
sysproperties.value AS PropertyValue,
syscolumns.name AS ColumnName,
syscolumns.colid AS Ordinal
FROM sysobjects INNER JOIN sysproperties
ON sysobjects.id = sysproperties.id
LEFT JOIN syscolumns
ON sysproperties.smallid = syscolumns.colid
AND sysproperties.id = syscolumns.id
ORDER BY SchemaOwner, ObjectName, ObjectType, Ordinal

The column 'ObjectType' can have a couple of different values: 'U'
(table), 'V' (view), and 'P' (procedure). If 'ColumnName' is filled,
it's an extended property of a field, if not, it's of the element
(table, view, proc).

Oracle:
Table/views:
SELECT * FROM ALL_TAB_COMMENTS WHERE OWNER = :schemaOwner ORDER BY
TABLE_TYPE ASC

// Table/view columns.
SELECT ALL_COL_COMMENTS.*,
ALL_TAB_COMMENTS.TABLE_TYPE
FROM ALL_COL_COMMENTS, ALL_TAB_COMMENTS
WHERE ALL_COL_COMMENTS.OWNER = :schemaOwner AND
ALL_COL_COMMENTS.TABLE_NAME=ALL_TAB_COMMENTS.TABLE_NAME
AND ALL_COL_COMMENTS.OWNER = ALL_TAB_COMMENTS.OWNER
ORDER BY ALL_COL_COMMENTS.TABLE_NAME ASC

This uses non-ansi joins so it also runs on Oracle 8i.

Frans

--
 
Frans,
OleDB has a method for that (see other posts) though it's very buggy:
it doesn't always give back results which are correct.
My snippet runs on SQLServer, I never tried it on OleDB

To inform the OP.

Cor
 
That's it - thank you

--
thanks - dave


Frans Bouma said:
OleDB has a method for that (see other posts) though it's very buggy:
it doesn't always give back results which are correct.

I pressume you want the 'Extended properties' for sqlserver and the
'Description' elements on oracle?

SqlServer:
SELECT sysobjects.Name AS ObjectName,
sysobjects.xtype AS ObjectType,
user_name(sysobjects.uid) AS SchemaOwner,
sysproperties.name AS PropertyName,
sysproperties.value AS PropertyValue,
syscolumns.name AS ColumnName,
syscolumns.colid AS Ordinal
FROM sysobjects INNER JOIN sysproperties
ON sysobjects.id = sysproperties.id
LEFT JOIN syscolumns
ON sysproperties.smallid = syscolumns.colid
AND sysproperties.id = syscolumns.id
ORDER BY SchemaOwner, ObjectName, ObjectType, Ordinal

The column 'ObjectType' can have a couple of different values: 'U'
(table), 'V' (view), and 'P' (procedure). If 'ColumnName' is filled,
it's an extended property of a field, if not, it's of the element
(table, view, proc).

Oracle:
Table/views:
SELECT * FROM ALL_TAB_COMMENTS WHERE OWNER = :schemaOwner ORDER BY
TABLE_TYPE ASC

// Table/view columns.
SELECT ALL_COL_COMMENTS.*,
ALL_TAB_COMMENTS.TABLE_TYPE
FROM ALL_COL_COMMENTS, ALL_TAB_COMMENTS
WHERE ALL_COL_COMMENTS.OWNER = :schemaOwner AND
ALL_COL_COMMENTS.TABLE_NAME=ALL_TAB_COMMENTS.TABLE_NAME
AND ALL_COL_COMMENTS.OWNER = ALL_TAB_COMMENTS.OWNER
ORDER BY ALL_COL_COMMENTS.TABLE_NAME ASC

This uses non-ansi joins so it also runs on Oracle 8i.

Frans

--
 
Miha said:
Hi,

I think that it is bad using system tables directly (instead of
proper stored procedures such as i.e. sp_tables)

Yes, I know, the proper way is to use INFORMATION_SCHEMA views. Though
these views are buggy for retrieval of meta-data for schema's other
than dbo if you're logged in as dbo. The code used is actually from one
of the INFORMATION_SCHEMA views and altered to make it work correctly.
This bug has been reported to MS but isn't fixed in the last SP4, so
that's why I kept the code.

FB

--
 
David,
Life would be so much simpler if the way we are supposed to do it would
always work...

Really?

Is it not often the challenge that it is not working (or that there is no
solution) and to find the answers for that, what most who come to these
newsgroups like?

:-)

Cor
 
Back
Top