zl程序教程

您现在的位置是:首页 >  数据库

当前栏目

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

数据库 获取 所有 名称 Access2000
2023-06-13 09:13:46 时间
voidOpenSchemaX(TCHAR*TableName)
{
HRESULThr=S_OK;

::CoInitialize(NULL);//初始化Com

IADORecordBinding*picRs=NULL;

_RecordsetPtrpRstSchema("ADODB.Recordset");
_ConnectionPtrpConnection("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))
{
CStringstrTableType;

_bstr_ttable_name=pRstSchema->Fields->
GetItem("TABLE_NAME")->Value;//获取表的名称

_bstr_ttable_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();
}
//Cleanupobjectsbeforeexit.

pRstSchema->Close();
pConnection->Close();
}

catch(_com_error&e)
{
//Notifytheuseroferrorsifany.
//PassaconnectionpointeraccessedfromtheConnection.
PrintProviderError(pConnection);
PrintComError(e);
}
CoUninitialize();
}

voidPrintProviderError(_ConnectionPtrpConnection)
{
ErrorPtrpErr=NULL;

if((pConnection->Errors->Count)>0)
{
longnCount=pConnection->Errors->Count;
//Collectionrangesfrom0tonCount-1.
for(longi=0;i<nCount;i++)
{
pErr=pConnection->Errors->GetItem(i);
CStringstrError;
strError.Format("Errornumber:%x\t%s",pErr->Number,pErr->Description);
AfxMessageBox(strError);
}
}
}

voidPrintComError(_com_error&e)
{
_bstr_tbstrSource(e.Source());
_bstr_tbstrDescription(e.Description());

//PrintCOMerrors.
CStringstrError;
strError.Format("Errornumber:Description=%s\tCodemeaning=%s",(LPCSTR)bstrDescription,e.ErrorMessage());
AfxMessageBox(strError);
}

调用方法:

CStringstrFileName;
TCHARFileName[MAX_PATH];
TCHARbigBuff[2048]=_T("");//maximumcommondialogbuffersize
TCHARszFilter[]=_T("TextFiles(*.mdb)|*.mdb|AllFiles(*.*)|*.*
");
CFileDialogdlg(TRUE,NULL,NULL,
OFN_HIDEREADONLY|OFN_ALLOWMULTISELECT,szFilter);

//ModifyOPENFILENAMEmembersdirectlytopointtobigBuff
dlg.m_ofn.lpstrFile=bigBuff;
dlg.m_ofn.nMaxFile=sizeof(bigBuff);

if(IDOK==dlg.DoModal())
{
strFileName=dlg.GetPathName();
lstrcpy(FileName,strFileName);
OpenSchemaX(FileName);
}(出处:风闪网路学院)