Primary and Foreign Keys Question


I have the following code that gets the primary and foreign keys from the
tables in my database. This works fine for tables that have only one
foreign key. My question is if a table has multiple foreign keys or
multiple columns make up one foreign key, will there be multiple rows coming
back from GetOleDbSchemaTable or will I have to look at several fields?

DataTable dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Foreign_Keys, null);

foreach (DataRow dr in dt.Rows)


// get primary/foreign table and column names

string pkTableName = BracketName((string)dr["PK_TABLE_NAME"]);

string fkTableName = BracketName((string)dr["FK_TABLE_NAME"]);

string pkColumnName = BracketName((string)dr["PK_COLUMN_NAME"]);

string fkColumnName = BracketName((string)dr["FK_COLUMN_NAME"]);





Each foreign key column will be represented as a row in the table that you
get back.

The easiest way to see this is to assign the table to a datagridview's

Kerry Moorman

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question