1. 程式人生 > >ACCESS 查詢表名許可權不夠,在 'MSYSOBJECTS' 上沒有讀取資料許可權

ACCESS 查詢表名許可權不夠,在 'MSYSOBJECTS' 上沒有讀取資料許可權

使用檢視所有表名語句:
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);
            //64位下無法使用Microsoft.Jet.OLEDB.4.0,會提示“未找到提供程式,……”
            //使用strconnection.Format(_T("Provider=Microsoft.ACE.OLEDB.12.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.Description(), L"錯誤", MB_OK);
        return _T("");
    }

}

 

這個段程式碼參考:https://blog.csdn.net/itmes/article/details/5213087