司空骏 發表於 2006-11-3 00:00:00

获取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-&gt;ConnectionString = TableName;<br>pConnection-&gt;Provider = "Microsoft.Jet.OLEDB.4.0";<br><br>try<br>{<br>pConnection-&gt;Open(pConnection-&gt;ConnectionString, "", "", adModeUnknown);<br>pRstSchema-&gt;QueryInterface(<br>__uuidof(IADORecordBinding), (LPVOID*)&amp;picRs);<br><br>pRstSchema = pConnection-&gt;OpenSchema(adSchemaTables);//枚举表的名称处理<br><br>while(!(pRstSchema-&gt;EndOfFile))<br>{<br>CString strTableType;<br><br>_bstr_t table_name = pRstSchema-&gt;Fields-&gt;<br>GetItem("TABLE_NAME")-&gt;Value;//获取表的名称<br><br>_bstr_t table_type = pRstSchema-&gt;Fields-&gt;<br>GetItem("TABLE_TYPE")-&gt;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-&gt;MoveNext();<br>}<br>// Clean up objects before exit.<br><br>pRstSchema-&gt;Close();<br>pConnection-&gt;Close();<br>}<br><br>catch (_com_error &amp;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-&gt;Errors-&gt;Count) &gt; 0)<br>{<br>long nCount = pConnection-&gt;Errors-&gt;Count;<br>// Collection ranges from 0 to nCount -1.<br>for(long i = 0;i &lt; nCount;i++)<br>{<br>pErr = pConnection-&gt;Errors-&gt;GetItem(i);<br>CString strError;<br>strError.Format("Error number: %x\t%s", pErr-&gt;Number, pErr-&gt;Description);<br>AfxMessageBox(strError);<br>}<br>}<br>}<br><br>void PrintComError(_com_error &amp;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]
查看完整版本: 获取ACCESS2000数据库中所有表的名称