获取数据库的表名并交其放入combobox的下拉列表中。程序无错,便是无法实现该功能,求大神指点
获取数据库的表名并交其放入combobox的下拉列表中。程序无错,就是无法实现该功能,求大神指点。
第一个程序:
BOOL CAboutDlg::GetTableName()BOOL CAboutDlg::GetTableName();
{
try {
_RecordsetPtr m_pRecordset=m_pConnection->OpenSchema(adSchemaTables);
while(!(m_pRecordset->adoEOF)) {
// 获取表的名称
_bstr_t table_name = m_pRecordset->Fields->GetItem("TABLE_NAME")->Value;
// 获取表的类型
_bstr_t table_type = m_pRecordset->Fields->GetItem("TABLE_TYPE")->Value;
// 添加表的名称
CComboBox combobox;
combobox.AddString((LPCSTR)table_name);//m_cmbTable是表名显示的地方
m_pRecordset->MoveNext();
}
m_pRecordset->Close();
} catch(_com_error e) {
// Notify the user of errors if any.
// Pass a connection pointer accessed from the Connection.
::MessageBox(NULL,e.Description(),"出错了!",MB_OK);
return FALSE;
}
}
第二个程序:
BOOL CAboutDlg::GetNameList()
{
CComboBox combobox;
try
{// m_pConnection must connected a database
_RecordsetPtr pRecordset = NULL;
SchemaEnum nSchemaEnum = adSchemaTables;
pRecordset = m_pConnection->OpenSchema(nSchemaEnum);
CString str;
while(!(pRecordset->adoEOF||pRecordset->BOF))
{
str = pRecordset->GetCollect("TABLE_TYPE").bstrVal;
str.MakeUpper();
if(strcmp(str,"TABLE")==0)
{
str = pRecordset->GetCollect("TABLE_NAME").bstrVal;
combobox.AddString(str);
}
else
{
str = pRecordset->GetCollect("TABLE_NAME").bstrVal;
if(str.Find("$")>0)
combobox.AddString(str);
}
pRecordset->MoveNext();
}//end while
if(pRecordset!=NULL)
pRecordset->Close();
}
catch (_com_error e)
{
return FALSE;
}
return TRUE;
}
------解决方案--------------------
把组合框关联上变量
第一个程序:
BOOL CAboutDlg::GetTableName()BOOL CAboutDlg::GetTableName();
{
try {
_RecordsetPtr m_pRecordset=m_pConnection->OpenSchema(adSchemaTables);
while(!(m_pRecordset->adoEOF)) {
// 获取表的名称
_bstr_t table_name = m_pRecordset->Fields->GetItem("TABLE_NAME")->Value;
// 获取表的类型
_bstr_t table_type = m_pRecordset->Fields->GetItem("TABLE_TYPE")->Value;
// 添加表的名称
CComboBox combobox;
combobox.AddString((LPCSTR)table_name);//m_cmbTable是表名显示的地方
m_pRecordset->MoveNext();
}
m_pRecordset->Close();
} catch(_com_error e) {
// Notify the user of errors if any.
// Pass a connection pointer accessed from the Connection.
::MessageBox(NULL,e.Description(),"出错了!",MB_OK);
return FALSE;
}
}
第二个程序:
BOOL CAboutDlg::GetNameList()
{
CComboBox combobox;
try
{// m_pConnection must connected a database
_RecordsetPtr pRecordset = NULL;
SchemaEnum nSchemaEnum = adSchemaTables;
pRecordset = m_pConnection->OpenSchema(nSchemaEnum);
CString str;
while(!(pRecordset->adoEOF||pRecordset->BOF))
{
str = pRecordset->GetCollect("TABLE_TYPE").bstrVal;
str.MakeUpper();
if(strcmp(str,"TABLE")==0)
{
str = pRecordset->GetCollect("TABLE_NAME").bstrVal;
combobox.AddString(str);
}
else
{
str = pRecordset->GetCollect("TABLE_NAME").bstrVal;
if(str.Find("$")>0)
combobox.AddString(str);
}
pRecordset->MoveNext();
}//end while
if(pRecordset!=NULL)
pRecordset->Close();
}
catch (_com_error e)
{
return FALSE;
}
return TRUE;
}
------解决方案--------------------
把组合框关联上变量