Examining a database schema from code (c#)

Posted on 2007-07-24
Last Modified: 2008-02-01
Hi all,

I'm writing an application in c# using an Sql 2005 database. The application needs to be able to examine the relationships and column constraints going on in the database and make decisions on based on them. What is the best way to go about doing this?

I know I can query the system schema tables but have so far only managed to find out very limited information about the various relationship between the tables and columns (like the column that has the constraint on it, the type (PK, FK, UC) of the constraint and it's name). Ideally I need to be able to do the following:

For any given table find all the columns that have constraints on them. If they are PK or FK constraints, find out info on the table and column(s) on the other side of the constraint. Is this possible.

Any help is greatly appreciated.

Question by:cjmos
    LVL 29

    Accepted Solution

    Funny but DBConnection class GetSchema method does not provide info you want.
    Still this is possible. have a look:
    LVL 21

    Assisted Solution

    There are some system tables in SQL server that contain all this information. You can write queries to get the information about the columns, constraints, etc
    Check out the following system tables
    Information_Schema.Tables, Information_Schema.Columns, Information_Schema.Constraints
    LVL 3

    Author Comment

    Thanks both for your solutions and my apologies for keeping this question open for so long. I actually managed to solve the problem a while ago by using the system tables (as mentioned by surajguptha) . I'm spliting the points as anarki_jimbel answered with a viable solution and because I kept the question open for so long.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK ( for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
    Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now