使用查看所有表名語句:
select name from MSysObjects where type=1 and flags=0
並不成功,用GetLastError();查看是沒有權限問題:
網上有修改mdb權限的方法,不過我的Access和一小部分網友一樣,找不到【工具】這一欄
後面找到另一種方式:
void GetTableLists(const CString &strPath)
{
_ConnectionPtr m_pConnection;
_RecordsetPtr m_pRecordset;
HRESULT hr;
CStringArray dbtables;
try
{
CoInitialize(NULL);
hr = m_pConnection.CreateInstance(__uuidof(Connection));
hr = m_pRecordset.CreateInstance(__uuidof(Recordset));
if (SUCCEEDED(hr))
{
CString strconnection;
strconnection.Format(_T("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s"), strPath);
hr = m_pConnection->Open((_bstr_t)strconnection, "", "", adModeUnknown);
m_pRecordset = m_pConnection->OpenSchema(adSchemaTables);
while (!(m_pRecordset->adoEOF))
{
_bstr_t table_name = m_pRecordset->Fields->GetItem("TABLE_NAME")->Value;
_bstr_t table_type = m_pRecordset->Fields->GetItem("TABLE_TYPE")->Value;
if (strcmp(((LPCSTR)table_type), "TABLE") == 0)
{
CString table;
table.Format(table_name);
dbtables.Add(table);
}
m_pRecordset->MoveNext();
}
m_pRecordset->Close();
}
m_pConnection->Close();
m_pRecordset.Release();
m_pConnection.Release();
CoUninitialize();
}
catch (_com_error e)
{
::MessageBox(NULL, e.ErrorMessage(), L"錯誤", MB_OK);
return _T("");
}
}