none
Get Primary Key and other indexes on a table in ODBC RRS feed

  • Question

  • I looked for a general database forum to no avail. The only database forum I found was TSQL specific. All the ODBC questions I found were in language fora so here I am.

    Given a table name on an ODBC database I access in C#, how do I get the Primary Key column name(s) and column names for other indexes on the table?

    Thanks,


    Jon Jacobs, There are 10 kinds of people: those who understand binary and those who don't

    • Moved by CoolDadTx Monday, March 13, 2017 9:20 PM ADO.NET related
    Monday, March 13, 2017 7:34 PM

Answers

  • ADO.NET is primarily for DML operations, not DDL stuff. If this is a SQL database then take a look at using SMO to get everything you need.

    For a generic database there is the GetSchema method on the DbConnection class. Unfortunately schema is specific to the DB and therefore if you want to support different DBs you'll have to write code to handle the differing schemas. MSDN has some documentation on this. For SQL the following additional schemas are available which includes FKs. But, again, if you are targeting SQL then just use SMO. It is a lot easier.

    Michael Taylor
    http://www.michaeltaylorp3.net

    Monday, March 13, 2017 9:20 PM

All replies

  • ADO.NET is primarily for DML operations, not DDL stuff. If this is a SQL database then take a look at using SMO to get everything you need.

    For a generic database there is the GetSchema method on the DbConnection class. Unfortunately schema is specific to the DB and therefore if you want to support different DBs you'll have to write code to handle the differing schemas. MSDN has some documentation on this. For SQL the following additional schemas are available which includes FKs. But, again, if you are targeting SQL then just use SMO. It is a lot easier.

    Michael Taylor
    http://www.michaeltaylorp3.net

    Monday, March 13, 2017 9:20 PM
  • Thank you very much. Right on the money!

    Jon Jacobs, There are 10 kinds of people: those who understand binary and those who don't

    Friday, March 17, 2017 11:38 AM