獲取ACCESS2000數(shù)據(jù)庫(kù)中所有表的名稱(chēng)
更新時(shí)間:2006年11月03日 00:00:00 作者:
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);//枚舉表的名稱(chēng)處理
while(!(pRstSchema->EndOfFile))
{
CString strTableType;
_bstr_t table_name = pRstSchema->Fields->
GetItem("TABLE_NAME")->Value;//獲取表的名稱(chēng)
_bstr_t table_type = pRstSchema->Fields->
GetItem("TABLE_TYPE")->Value;//獲取表的類(lèi)型
strTableType.Format("%s",(LPCSTR) table_type);
if(!lstrcmp(strTableType,_T("TABLE")))
{
m_strList.AddString((LPCSTR) table_name);//添加表的名稱(chēng)
}
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);
}
調(diào)用方法:
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);
}(出處:風(fēng)閃網(wǎng)路學(xué)院)
相關(guān)文章
自己動(dòng)手把ACCESS轉(zhuǎn)換到SQLSERVER的方法
這篇文章主要介紹了自己動(dòng)手把ACCESS轉(zhuǎn)換到SQLSERVER方法,需要的朋友可以參考下2014-08-08Access數(shù)據(jù)庫(kù)不能寫(xiě)不能更新的解決方法
本文提供了Access數(shù)據(jù)庫(kù)不能寫(xiě)不能更新的解決方法,主要是權(quán)限原因,具體看下面的具體解決方法2013-11-11恢復(fù)從 Access 2000、 Access 2002 或 Access 2003 中數(shù)據(jù)庫(kù)刪除表的方法
恢復(fù)從 Access 2000、 Access 2002 或 Access 2003 中數(shù)據(jù)庫(kù)刪除表的方法...2007-08-08Access 模糊參數(shù) 分頁(yè)查詢(xún)
access下 模糊查詢(xún)方法,值得大家參考學(xué)習(xí)。2009-06-06Access連接數(shù)據(jù)源(ODBC)配置(新手必知)
win7 64位旗艦版控制面板中管理工具下的數(shù)據(jù)源(ODBC)配置竟然只有SQLServer的驅(qū)動(dòng),其他的都沒(méi)有了,此問(wèn)題的解決方法如下2014-07-07讀取注冊(cè)表根據(jù)Office版本獲取數(shù)據(jù)庫(kù)連接字段
本節(jié)主要介紹了如何根據(jù)Office版本獲取數(shù)據(jù)庫(kù)連接字段,以讀取注冊(cè)表獲取Office版本,實(shí)現(xiàn)代碼如下,感興趣的朋友不要錯(cuò)過(guò)2014-07-07