一小段利用adox获取表结构主键字段的代码

来源:互联网 发布:java socket传输中断 编辑:程序博客网 时间:2024/05/16 01:50


#import "c:/Program Files/Common Files/system/ado/msadox.dll"    no_namespace
#import "c:/Program Files/Common Files/system/ado/msado15.dll"
#include <stdio.h>

int main()
{
 if(FAILED(::CoInitialize(NULL)))
  return 1;
    _CatalogPtr pCatalog   = NULL;

    _bstr_t strcnn("Provider=SQLOLEDB.1;Password=xxxx;Persist Security Info=True;"
  "User ID=sa;Initial Catalog=pubs;Data Source=server");

 pCatalog.CreateInstance(__uuidof (Catalog));
 pCatalog->PutActiveConnection(strcnn);

 _variant_t varIndex((long)-1);
 _KeyPtr pKey = NULL;
 KeyTypeEnum enumKey ;
 try
 {
  while (1)//i don't how to decide the key's cnt,so just make a throw error
  {
   ++varIndex.llVal;
   pKey = pCatalog->Tables->GetItem("titleauthor")->Keys->GetItem(varIndex);
   static const char *KEYDESC [] = {"adKeyPrimary ","adKeyForeign ","adKeyUnique "};
   enumKey = pKey->GetType ();
   printf("%s/t%d/t%s/r/n",(char*)pKey->GetName (),enumKey,KEYDESC[enumKey-1]);
  }

 }
 catch(...)
 {
 }
 ::CoUninitialize();
 return 0;
}

原创粉丝点击