Click here to Skip to main content
15,887,485 members
Articles / Programming Languages / C#
Tip/Trick

Extract All Schema Information From Your Database

Rate me:
Please Sign up or sign in to vote.
3.20/5 (3 votes)
16 Apr 2015CPOL 10.1K   9   1
how to get as much information about your database as possible via ADO.NET

Introduction

This little code snippet shows how to build a DataSet with 10+ DataTables filled with details about your database.

Using the Code

All you need is an active DbConnection-Object and then call the following method:

C#
using System.Data;
using System.Data.Common;


static DataSet getFullSchemaInfo(DbConnection connection)
{
    DataSet result = new DataSet("Schema Info");
    DataTable dt;

    if (connection.State == ConnectionState.Closed)
        connection.Open();

    dt = connection.GetSchema(DbMetaDataCollectionNames.MetaDataCollections);
    result.Tables.Add(dt);

    foreach (DataRow row in dt.Rows)
    {
        string collection = row[0].ToString();

        if (collection != DbMetaDataCollectionNames.MetaDataCollections)
        {
            try
            {
                DataTable dt2 = connection.GetSchema(collection);
                result.Tables.Add(dt2);
            }
            catch (Exception)
            {
                // some Data-Providers have problems when you grab some collections 
		// (e.g. "Indexes") without restrictions
                // hey ODBC, I'm looking at you!
                ;
            }
        }
    }
    return result;
}

The DataSet will contain at least the tables "MetaDataCollections", "DataSourceInformation", "DataTypes", "Restrictions" and "ReservedWords".

Typically, there will be also some more tables like "Columns", "Procedures", "ProcedureColumns", "Tables", "Views".

Just bind these tables to a DataGridView or any other data grid and you will can see this bunch of information.

History

  • 2015-04-14 - Initial release

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 4 Pin
Member 188040320-Apr-15 1:34
Member 188040320-Apr-15 1:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.