获取ACCESS2000数据库中的所有表的名称(vc + ado)

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

本文简介:选择自 ado_database 的 blog

void openschemax(tchar *tablename) { hresulthr = s_ok; ::coinitialize(null); //初始化com iadorecordbinding *picrs = null; _recordsetptr prstschema("adodb.recordset"); _connectionptr pconnection("adodb.connection" ); pconnection->connectionstring = tablename; pconnection->provider = "microsoft.jet.oledb.4.0"; try { pconnection->open(pconnection->connectionstring, "", "", admodeunknown); prstschema->queryinterface(  __uuidof(iadorecordbinding), (lpvoid*)&picrs); prstschema = pconnection->openschema(adschematables);//枚举表的名称处理while(!(prstschema->endoffile)) {  cstring strtabletype;  _bstr_t table_name = prstschema->fields-> getitem("table_name")->value;//获取表的名称 _bstr_t table_type = prstschema->fields-> getitem("table_type")->value;//获取表的类型  strtabletype.format("%s",(lpcstr) table_type);   if(!lstrcmp(strtabletype,_t("table"))) { m_strlist.addstring((lpcstr) table_name);//添加表的名称 }  prstschema->movenext(); } // clean up objects before exit. prstschema->close(); pconnection->close(); } catch (_com_error &e) { // notify the user of errors if any. // pass a connection pointer accessed from the connection.printprovidererror(pconnection); printcomerror(e); } couninitialize(); } void printprovidererror(_connectionptr pconnection) { errorptrperr= null; if( (pconnection->errors->count) > 0) { long ncount = pconnection->errors->count; // collection ranges from 0 to ncount -1. for(long i = 0;i < ncount;i++) { perr = pconnection->errors->getitem(i);  cstring strerror;  strerror.format("error number: %x\t%s", perr->number, perr->description);  afxmessagebox(strerror); } } } void printcomerror(_com_error &e) { _bstr_t bstrsource(e.source()); _bstr_t bstrdescription(e.description()); // print com errors. cstring strerror; strerror.format("error number: description = %s\tcode meaning = %s",(lpcstr) bstrdescription, e.errormessage()); afxmessagebox(strerror); } 调用方法: cstring strfilename; tchar filename[max_path]; tchar bigbuff[2048] = _t("");// maximum common dialog buffer size tchar szfilter[] = _t("text files (*.mdb)|*.mdb|all files (*.*)|*.*||"); cfiledialog dlg(true, null, null, ofn_hidereadonly | ofn_allowmultiselect, szfilter); // modify openfilename members directly to point to bigbuff dlg.m_ofn.lpstrfile = bigbuff; dlg.m_ofn.nmaxfile = sizeof(bigbuff); if(idok == dlg.domodal() ) { strfilename = dlg.getpathname(); lstrcpy(filename,strfilename); openschemax(filename); } void cado5dlg::onbutton2() { // todo: add your control notification handler code here hresult hr = s_ok; coinitialize(null); iadorecordbinding *picrs = null; _recordsetptr prstschema("adodb.recordset"); _connectionptr pconnection("adodb.connection"); try { pconnection->open( "provider=microsoft.jet.oledb.4.0;data source=ado1.mdb;", "", "", admodeunknown); prstschema->queryinterface(__uuidof(iadorecordbinding), (lpvoid*)& picrs); prstschema = pconnection->openschema(adschematables); while(!(prstschema->adoeof)) { cstring strtabletype; _bstr_t table_name = prstschema->fields->getitem("table_name")->value; _bstr_t table_type = prstschema->fields->getitem("table_type")->value; strtabletype.format("%s",(lpcstr)table_type); if (!lstrcmp(strtabletype, _t("table"))) { m_listbox.addstring((lpcstr)table_name); } prstschema->movenext(); } prstschema->close(); pconnection->close(); } catch(_com_error &e) { afxmessagebox("error!"); } couninitialize(); }

本文关键:获取ACCESS2000数据库中的所有表的名称(vc + ado)
 

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

go top