遍历用户表的代码:
CoInitialize(NULL);
_ConnectionPtr pconn(__uuidof(Connection));
_RecordsetPtr prs(__uuidof(Recordset));
pconn->ConnectionString=\"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\\\VC\\\\Lesson20\\\\db.mdb;Persist Security Info=False\";
pconn->Open(\"\",\"\",\"\",adConnectUnspecified);
prs=pconn->OpenSchema(adSchemaTables);
while (!prs->adoEoF)
{
if (!strcmp((_bstr_t)prs->Fields->GetItem(\"TABLE_TYPE\")->Value,\"TABLE\"))
{
m_tablelist.AddString((_bstr_t)prs->Fields->GetItem(\"TABLE_NAME\")->Value);
prs->MoveNext();
}
else
prs->MoveNext();
}
prs->Close();
pconn->Close();
prs.Release();
pconn.Release();
CoUninitialize();
遍历字段的代码:
CString str;
m_tablelist.GetText(m_tablelist.GetCurSel(),str);//通过表列表框得到表的名称
if (m_columnlist.GetCount()>0)
{
m_columnlist.ResetContent();
}
CoInitialize(NULL);
_ConnectionPtr pconn(__uuidof(Connection));
_RecordsetPtr prs(__uuidof(Recordset));
pconn->ConnectionString=\"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\\\VC\\\\Lesson20\\\\db.mdb;Persist Security Info=False\";
pconn->Open(\"\",\"\",\"\",adConnectUnspecified);
prs=pconn->OpenSchema(adSchemaColumns);
while (!prs->adoEoF)
{
if (!strcmp((_bstr_t)prs->Fields->GetItem(\"TABLE_NAME\")->Value,(_bstr_t)(LPCTSTR)str))
{
int index=m_columnlist.GetCount();
// m_columnlist.AddString((_bstr_t)prs->Fields->GetItem(\"COLUMN_NAME\")->Value);
m_columnlist.InsertString(index,(_bstr_t)prs->Fields->GetItem(\"COLUMN_NAME\")->Value);
prs->MoveNext();
}
else
prs->MoveNext();
}
prs->Close();
pconn->Close();
prs.Release();
pconn.Release();
CoUninitialize();