ADO实现Access数据库表的遍历和字段的遍历
欢迎进入C/C++编程社区论坛,与200万技术人员互动交流 >>进入 网络上鲜有VC版的相关代码,今日学习了VC利用ADO操纵数据库部分。查了一下MSDN写了以下代: 遍历用户表的代码: CoInitialize(NULL); _ConnectionPtr pconn(__uuidof(Connection)); _RecordsetPtr
欢迎进入C/C++编程社区论坛,与200万技术人员互动交流 >>进入
网络上鲜有VC版的相关代码,今日学习了VC利用ADO操纵数据库部分。查了一下MSDN写了以下代:遍历用户表的代码:
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();