is it primary key or not ? 
Author Message
 is it primary key or not ?

How to fine out a table field is primary key or nor using Vc++ with ADO/ADOX

Wed, 20 Oct 2004 14:12:29 GMT  
 is it primary key or not ?

It's pretty straightforward to iterate through the Keys Collection for a
specific table.  With that, you can search the collection for a Key of type
adPrimaryKey.  When you have that key, you can search the keys Column Collection
to see if the column is listed.  Note that a primary key (as well as most
indexes) can consist of multiple columns - so a column may be in a PK
constraint, either singular or with other columns, in the case of a composite
PK).  I have thrown together a very quick console app that will list all of the
keys of a particular table (AccountMaster in this case) for an MS Access

I hope this helps.

best regards,
roy fine

/* ******************************************************* */
int main(int argc, char* argv[]){

ADODB::_ConnectionPtr cnxion;
ADOX::_CatalogPtr catlg;
ADOX::_KeyPtr key;
ADOX::KeysPtr keys;




keys = catlg->GetTables()->GetItem(L"AccountMaster")->GetKeys();
int kcnt = keys->GetCount();
for(int i=0; i<kcnt; i++){
 key = keys->GetItem((short)i);
 _bstr_t keyName = key->GetName();
 ADOX::KeyTypeEnum ktyp = key->GetType();
 char *ktypStr = ktyp==ADOX::adKeyPrimary?"PrimaryKey":"OtherType";
 printf("Key %d: name:%s, type:%s\n",i,(char*)keyName,ktypStr);

 ADOX::ColumnsPtr cols;
 ADOX::_ColumnPtr col;
 cols = key->GetColumns();
 int kcolCnt = cols->GetCount();
 for(int j=0; j<kcolCnt; j++){
  col = cols->GetItem((short)j);
  _bstr_t colName = col->GetName();
  printf("\t\tColumn %d: name:%s\n",j,(char*)colName);

printf("Hello World!\n");
return 0;


> How to fine out a table field is primary key or nor using Vc++ with ADO/ADOX

Thu, 21 Oct 2004 03:13:46 GMT  
 [ 2 post ] 

 Relevant Pages 

1. AddNew does not increment my AutIncr Primary Key

2. Why am I not intercepting Enter key (VK_RETURN)

3. Can't delete rows from a table in SQL Server 2000 without a primary key

4. auto-increment the primary key with datasets

5. Primary key

6. getting primary key with ExecuteNonQuery

7. How can I get Primary Key from mdb ??

8. can't create primary key

9. Primary key, ATL

10. primary/foreign keys using OLE DB / COM

11. Primary/foreign keys using OLE DB

12. Auto ID Primary Key after AddNew


Powered by phpBB® Forum Software