代码如下:(说明:一个列表框m_Accesslist。数据库:学生.mdb,表名:会员列表,字段名有两个,分别是“姓名”和“性别”)
void CWwwDlg::OnOK()
{
// TODO: Add extra validation here
::CoInitialize(NULL);
_RecordsetPtr m_pRecordset;
_ConnectionPtr m_pConnection;
m_pRecordset.CreateInstance(__uuidof(Recordset));
m_pConnection.CreateInstance(__uuidof(Connection));
////////↓↓↓↓连接数据库↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
try
{
m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=学生.mdb","","",adModeUnknown);
}
catch(_com_error e)
{
AfxMessageBox("请确定数据库在同一目录"); }
////////////////////////////////////////////////////////////////////
/////////打开数据表↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓ _bstr_t bstr;
bstr="select * from 会员列表";
try
{ m_pRecordset=m_pConnection->Execute(bstr,NULL,adCmdText); }
catch(_com_error& e)
{
CString strValue;
strValue.Format(_T("%s"),(LPCTSTR)e.Description());
AfxMessageBox(strValue);
return;
}
///////////////////////////////////////////////////////////////////////////////////////
//////////读取表内数据↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
_variant_t var;
CString strName,strAge;
while(!m_pRecordset->adoEOF)
{
var=m_pRecordset->GetCollect("姓名");
if(var.vt != VT_NULL)
strName = (LPCSTR)_bstr_t(var);
var = m_pRecordset->GetCollect("性别");
if(var.vt != VT_NULL)
strAge = (LPCSTR)_bstr_t(var);
m_Accesslist.AddString( strName + " --> "+strAge );
m_pRecordset->MoveNext();
} m_Accesslist.SetCurSel(0); m_pRecordset->Close();
m_pConnection->Close();
m_pRecordset.Release();
m_pConnection.Release(); CoUninitialize(); //CDialog::OnOK();
}问题是:
现在都把代码都放在IDOK里,运行是没有问题。可是我想在对话框初始化的时候就连接数据库,然后只让IDOK取出数据表内的信息并显示在列表框里!我就把上面关于连接数据库的代码放进了BOOL CWwwApp::InitInstance()这里,问题出现了!数据库是能连接上,但是打开数据表的时候出错!
然后我又把关于打开数据表的代码放在BOOL CWwwDlg::OnInitDialog()这里,还是打开数据表的时候出错!请问这是为什么啊??
void CWwwDlg::OnOK()
{
// TODO: Add extra validation here
::CoInitialize(NULL);
_RecordsetPtr m_pRecordset;
_ConnectionPtr m_pConnection;
m_pRecordset.CreateInstance(__uuidof(Recordset));
m_pConnection.CreateInstance(__uuidof(Connection));
////////↓↓↓↓连接数据库↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
try
{
m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=学生.mdb","","",adModeUnknown);
}
catch(_com_error e)
{
AfxMessageBox("请确定数据库在同一目录"); }
////////////////////////////////////////////////////////////////////
/////////打开数据表↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓ _bstr_t bstr;
bstr="select * from 会员列表";
try
{ m_pRecordset=m_pConnection->Execute(bstr,NULL,adCmdText); }
catch(_com_error& e)
{
CString strValue;
strValue.Format(_T("%s"),(LPCTSTR)e.Description());
AfxMessageBox(strValue);
return;
}
///////////////////////////////////////////////////////////////////////////////////////
//////////读取表内数据↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
_variant_t var;
CString strName,strAge;
while(!m_pRecordset->adoEOF)
{
var=m_pRecordset->GetCollect("姓名");
if(var.vt != VT_NULL)
strName = (LPCSTR)_bstr_t(var);
var = m_pRecordset->GetCollect("性别");
if(var.vt != VT_NULL)
strAge = (LPCSTR)_bstr_t(var);
m_Accesslist.AddString( strName + " --> "+strAge );
m_pRecordset->MoveNext();
} m_Accesslist.SetCurSel(0); m_pRecordset->Close();
m_pConnection->Close();
m_pRecordset.Release();
m_pConnection.Release(); CoUninitialize(); //CDialog::OnOK();
}问题是:
现在都把代码都放在IDOK里,运行是没有问题。可是我想在对话框初始化的时候就连接数据库,然后只让IDOK取出数据表内的信息并显示在列表框里!我就把上面关于连接数据库的代码放进了BOOL CWwwApp::InitInstance()这里,问题出现了!数据库是能连接上,但是打开数据表的时候出错!
然后我又把关于打开数据表的代码放在BOOL CWwwDlg::OnInitDialog()这里,还是打开数据表的时候出错!请问这是为什么啊??
::CoInitialize(NULL);
这一句,不仅放在InitInstance,在OnInitDialog的开始处也放上
试试看究其原因::CoInitialize(NULL);初始化的是单线程套间(Apartment),也就是STA,这样的话,
你只能在单线程中使用COM对象。::CoInitializeEx(NULL)
用这个替代试试看
建议尽快关闭连接,以避免占用服务器资源。
数据库访问代码要加异常处理。
应该是这句话出现了问题,我以前碰到过类似的问题。改为
m_pRecordset->Open(bstrSQL, m_pConnection.GetInterfacePtr(), adOpenStatic, adLockOptimistic, adCmdText);
试试 这样返回的结果集可以处理。
1。在
BOOL CWwwApp::InitInstance()
{。。
//COM库的初始化
AfxOleInit();
RecordsetPtr m_pRecordset;
_ConnectionPtr m_pConnection;
m_pRecordset.CreateInstance(__uuidof(Recordset));
m_pConnection.CreateInstance(__uuidof(Connection));
////////↓↓↓↓连接数据库↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
try
{
m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=学生.mdb","","",adModeUnknown);
}
catch(_com_error e)
{
AfxMessageBox("请确定数据库在同一目录"); }
}
原来还是在打开数据表那个地方!我是这样改的
BOOL CWwwApp::InitInstance()
{
…… AfxOleInit();
m_pConnection.CreateInstance(__uuidof(Connection));
////////↓↓↓↓连接数据库↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
try
{
m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=学生.mdb","","",adModeUnknown);
}
catch(_com_error e)
{
AfxMessageBox("请确定数据库在同一目录"); }
////////////////////////////////////////////////////////////////////
……
}BOOL CWwwDlg::OnInitDialog()
{
extern CWwwApp theApp; //注意这里,加了这句
m_pRecordset.CreateInstance(__uuidof(Recordset));
try
{
m_pRecordset->Open("SELECT * FROM 会员列表", theApp.m_pConnection.GetInterfacePtr(), // 这里用theApp(获取库接库的IDispatch指针)
adOpenDynamic,
adLockOptimistic,
adCmdText);
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
}}
void CWwwDlg::OnOK()
{
// TODO: Add extra validation here
_variant_t var;
CString strName,strAge;
m_Accesslist.ResetContent();
strName=strAge="";
try
{
if(!m_pRecordset->BOF)
m_pRecordset->MoveFirst();
else
{
AfxMessageBox("表内数据为空");
return;
} while(!m_pRecordset->adoEOF)
{
var = m_pRecordset->GetCollect("姓名");
if(var.vt != VT_NULL)
strName = (LPCSTR)_bstr_t(var);
var = m_pRecordset->GetCollect("性别");
if(var.vt != VT_NULL)
strAge = (LPCSTR)_bstr_t(var); m_Accesslist.AddString( strName + " --> "+strAge ); m_pRecordset->MoveNext();
} m_Accesslist.SetCurSel(0); }
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
}
//CDialog::OnOK();
}
请问为什么要加extern CWwwApp theApp;这句啊?
我看好多关于ADO教程里都没有这句啊!
我基础不好,现在正重新看VC教程,所以大家麻烦说的详细点啊!多谢大家的帮忙啊!谢谢,谢谢!