结合ADO、ADOX和MFC的文档/视图/框架架构创建和打开Access数据库[2]

[入库:2005年8月18日] [更新:2007年3月24日]

本文简介:选择自 mynote 的 blog

编写一个辅助函数,用于创建数据库、表和索引
bool cpassportdoc::createdb(lpctstr lpszfile)
{
 if(::pathfileexists(lpszfile)){
  cstring strtemp;
  strtemp.format(ids_target_exists,lpszfile);
  afxmessagebox(lpszfile);
  return false;
 }
 adodb::_connectionptr tempconnn;
 adox::_catalogptr pcatalog = null;
 adox::_tableptr  ptable = null;
    adox::_indexptr pindexnew  = null;
    adox::_indexptr pindex  = null;
 cstring strconnect;
 cstring strdbpath=lpszfile;
 strconnect.format(_t("provider=microsoft.jet.oledb.4.0;data source=%s"),strdbpath);
 colevariant connect(strconnect);
 try{
  pcatalog.createinstance(_t("adox.catalog"));
  pcatalog->create((lpctstr)strconnect);//创建数据库
  tempconnn.createinstance(_t("adodb.connection"));
  tempconnn->putcommandtimeout(30);
  tempconnn->putconnectiontimeout(30);
  tempconnn->put_cursorlocation(adodb::aduseclient);
  tempconnn->open(_bstr_t(strconnect),_bstr_t(),_bstr_t(),adodb::adconnectunspecified);
  pcatalog->putactiveconnection(_variant_t((idispatch *) tempconnn));
  ptable.createinstance(_t("adox.table"));
  ptable->parentcatalog =pcatalog;
  ptable->name="passport";
  adox::columnsptr pcols =ptable->columns;
  pcols->append(_t("recordid")  ,adox::adinteger,0);//自动编号字段
  pcols->append(_t("name")   ,adox::adwchar,255);//文本字段
  pcols->append(_t("dateofbirth")  ,adox::addate,0);//日期字段
  pcols->append(_t("otherinfo"),adox::adlongvarwchar,0);//备注字段
  pcatalog->tables->refresh();
  long lcount=pcols->count;
  for(long i=0;i<lcount;i++){
   pcols->getitem(i)->parentcatalog =pcatalog;//重要!设置catalog,参见q201826 prb: error 3265 when you access properties collection
   adox::propertiesptr pproperties=pcols->getitem(i)->properties;
   if(pproperties){//这里是用于调试的属性显示代码
    long lp=pproperties->count;
    trace("properties for col %s\r\n",(lpctstr)pcols->getitem(i)->name);
    for(long j=0;j<lp;j++){
     trace("\rproperty %s:%s\r\n",g_getvaluestring(pproperties->getitem(j)->name)
      ,g_getvaluestring(pproperties->getitem(j)->value));
    }
   }
  }
  pcols->getitem(_t("recordid"))->properties->getitem(_t("description"))->value=_t("记录编号");//注释
  pcols->getitem(_t("recordid"))->properties->getitem(_t("autoincrement"))->value=true;//自动编号
  pcols->getitem(_t("name"))->properties->getitem(_t("jet oledb:compressed unicode strings"))->value=true;
  pcols->getitem(_t("name"))->properties->getitem(_t("description"))->value=_t("姓名");
  pcols->getitem(_t("dateofbirth"))->properties->getitem(_t("description"))->value=_t("出生日期");
 pcols->getitem(_t("otherinfo"))->properties->getitem(_t("jet oledb:compressed unicode strings"))->value=true;
  pcols->getitem(_t("otherinfo"))->properties->getitem(_t("description"))->value=_t("其他信息");
  pcatalog->tables->append(_variant_t ((idispatch*)ptable));//添加表
  pcatalog->tables->refresh();//刷新
  pindexnew.createinstance(_t("adox.index"));
  pindexnew->name = "recordid";//索引名称
  pindexnew->columns->append("recordid",adox::adinteger,0);//索引字段
  pindexnew->putprimarykey(-1);//主索引
  pindexnew->putunique(-1);//唯一索引
  ptable->indexes->append(_variant_t ((idispatch*)pindexnew));//创建索引
  pindexnew=null;
  pcatalog->tables->refresh();//刷新
  return true;
 }
 catch(_com_error &e){
  eserrprintprovidererror(tempconnn);
  eserrprintcomerror(e);
  return false;
 }
 catch(...){
 }
 return false;
}
辅助的数据库函数。由于这些函数是jiangsheng以前为一个项目写的。所以命名有些奇怪。借鉴了mfc类cdaorecordset的部分代码
#define _countof(array) (sizeof(array)/sizeof(array[0]))
bool esrecordsetopen(
   lpctstr lpszsql
   ,adodb::_connectionptr pconnection
   ,adodb::_recordsetptr& rst
   ,adodb::cursortypeenum cursortype//=adopendynamic
&n

本文关键:结合ADO、ADOX和MFC的文档/视图/框架架构创建和打开Access数据库
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top