Primary and Foreign Keys Question

  • Thread starter Thread starter news.microsoft.com
  • Start date Start date
N

news.microsoft.com

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"]);



Thanks

Bill
 
Bill,

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
datasource.

Kerry Moorman
 
Back
Top