获取ACCESS2000数据库中所有表的名称
<br>void OpenSchemaX(TCHAR *TableName)<br>{<br>HRESULT hr = S_OK;<br><br>::CoInitialize(NULL); //初始化Com<br><br>IADORecordBinding *picRs = NULL;<br><br>_RecordsetPtr pRstSchema("ADODB.Recordset");<br>_ConnectionPtr pConnection("ADODB.Connection" );<br><br>pConnection->ConnectionString = TableName;<br>pConnection->Provider = "Microsoft.Jet.OLEDB.4.0";<br><br>try<br>{<br>pConnection->Open(pConnection->ConnectionString, "", "", adModeUnknown);<br>pRstSchema->QueryInterface(<br>__uuidof(IADORecordBinding), (LPVOID*)&picRs);<br><br>pRstSchema = pConnection->OpenSchema(adSchemaTables);//枚举表的名称处理<br><br>while(!(pRstSchema->EndOfFile))<br>{<br>CString strTableType;<br><br>_bstr_t table_name = pRstSchema->Fields-><br>GetItem("TABLE_NAME")->Value;//获取表的名称<br><br>_bstr_t table_type = pRstSchema->Fields-><br>GetItem("TABLE_TYPE")->Value;//获取表的类型<br><br>strTableType.Format("%s",(LPCSTR) table_type);<br><br>if(!lstrcmp(strTableType,_T("TABLE")))<br>{<br>m_strList.AddString((LPCSTR) table_name);//添加表的名称<br>}<br><br>pRstSchema->MoveNext();<br>}<br>// Clean up objects before exit.<br><br>pRstSchema->Close();<br>pConnection->Close();<br>}<br><br>catch (_com_error &e)<br>{<br>// Notify the user of errors if any.<br>// Pass a connection pointer accessed from the Connection. <br>PrintProviderError(pConnection);<br>PrintComError(e);<br>}<br>CoUninitialize();<br>}<br><br>void PrintProviderError(_ConnectionPtr pConnection)<br>{<br>ErrorPtr pErr = NULL;<br><br>if( (pConnection->Errors->Count) > 0)<br>{<br>long nCount = pConnection->Errors->Count;<br>// Collection ranges from 0 to nCount -1.<br>for(long i = 0;i < nCount;i++)<br>{<br>pErr = pConnection->Errors->GetItem(i);<br>CString strError;<br>strError.Format("Error number: %x\t%s", pErr->Number, pErr->Description);<br>AfxMessageBox(strError);<br>}<br>}<br>}<br><br>void PrintComError(_com_error &e)<br>{<br>_bstr_t bstrSource(e.Source());<br>_bstr_t bstrDescription(e.Description());<br><br>// Print COM errors. <br>CString strError;<br>strError.Format("Error number: Description = %s\tCode meaning = %s",(LPCSTR) bstrDescription, e.ErrorMessage());<br>AfxMessageBox(strError);<br>}<br><br>调用方法:<br><br>CString strFileName;<br>TCHAR FileName;<br>TCHAR bigBuff = _T(""); // maximum common dialog buffer size<br>TCHAR szFilter[] = _T("Text Files (*.mdb)|*.mdb|All Files (*.*)|*.*<br>");<br>CFileDialog dlg(TRUE, NULL, NULL,<br>OFN_HIDEREADONLY | OFN_ALLOWMULTISELECT, szFilter);<br><br>// Modify OPENFILENAME members directly to point to bigBuff<br>dlg.m_ofn.lpstrFile = bigBuff;<br>dlg.m_ofn.nMaxFile = sizeof(bigBuff);<br><br>if(IDOK == dlg.DoModal() )<br>{<br>strFileName = dlg.GetPathName();<br>lstrcpy(FileName,strFileName);<br>OpenSchemaX(FileName);<br>}(出处:风闪网路学院)
頁:
[1]