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/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);
}
调用方法:
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);
}(出处:风闪网路学院)
新闻热点
疑难解答