当前位置:首页 > 网络编程 > 数据库 > ACCESS > 获取ACCESS2000数据库中所有表的名称

获取ACCESS2000数据库中所有表的名称

点击次数:47 次 发布日期:2008-11-21 22:43:41 作者:源代码网
源代码网推荐

源代码网整理以下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);//枚举表的名称处理

源代码网整理以下while(!(pRstSchema->EndOfFile))
{
CString strTableType;

源代码网整理以下_bstr_t table_name = pRstSchema->Fields->
GetItem("TABLE_NAME")->Value;//获取表的名称

源代码网整理以下_bstr_t table_type = pRstSchema->Fields->
GetItem("TABLE_TYPE")->Value;//获取表的类型

源代码网整理以下strTableType.Format("%s",(LPCSTR) table_type);

源代码网整理以下if(!lstrcmp(strTableType,_T("TABLE")))
{
m_strList.AddString((LPCSTR) table_name);//添加表的名称
}

源代码网整理以下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 %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 Code meaning = %s",(LPCSTR) bstrDescription, e.ErrorMessage());
AfxMessageBox(strError);
}

源代码网整理以下调用方法:

源代码网整理以下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);
}(出处:风闪网路学院)

源代码网供稿.
网友评论 (0)
会员中心
网络编程
本站推荐
网络编程之精华