国内最全IT社区平台 联系我们 | 收藏本站
华晨云阿里云优惠2
您当前位置:首页 > 数据库 > access > 获取ACCESS2000数据库中所有表的名称

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

来源:程序员人生   发布时间:2013-12-26 22:21:57 阅读次数:3018次

void OpenSchemaX(TCHAR *TableName)
{
HRESULT hr = S_OK;

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

IADORecordBinding *picRs = NULL;

_RecordsetPtr pRstSchema(ADODBRecordset);
_ConnectionPtr pConnection(ADODBConnection );

pConnection>ConnectionString = TableName;
pConnection>Provider = MicrosoftJetOLEDB;

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;//获取表的类型

strTableTypeFormat(%s(LPCSTR) table_type);

if(!lstrcmp(strTableType_T(TABLE)))
{
m_strListAddString((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) > )
{
long nCount = pConnection>Errors>Count;
// Collection ranges from to nCount
for(long i = ;i < nCount;i++)
{
pErr = pConnection>Errors>GetItem(i);
CString strError;
strErrorFormat(Error number: %x %s pErr>Number pErr>Description);
AfxMessageBox(strError);
}
}
}

void PrintComError(_com_error &e)
{
_bstr_t bstrSource(eSource());
_bstr_t bstrDescription(eDescription());

// Print COM errors
CString strError;
strErrorFormat(Error number: Description = %s Code meaning = %s(LPCSTR) bstrDescription eErrorMessage());
AfxMessageBox(strError);
}

调用方法

CString strFileName;
TCHAR FileName[MAX_PATH];
TCHAR bigBuff[] = _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
dlgm_ofnlpstrFile = bigBuff;
dlgm_ofnnMaxFile = sizeof(bigBuff);

if(IDOK == dlgDoModal() )
{
strFileName = dlgGetPathName();
lstrcpy(FileNamestrFileName);
OpenSchemaX(FileName);
}(出处风闪网路学院)
生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠
程序员人生
------分隔线----------------------------
分享到:
------分隔线----------------------------
关闭
程序员人生