代码如下:(说明:一个列表框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()这里,还是打开数据表的时候出错!请问这是为什么啊??

解决方案 »

  1.   

    我就把上面关于连接数据库的代码放进了BOOL CWwwApp::InitInstance()这里,问题出现了!数据库是能连接上,但是打开数据表的时候出错! Re: 
    ::CoInitialize(NULL);
    这一句,不仅放在InitInstance,在OnInitDialog的开始处也放上
    试试看究其原因::CoInitialize(NULL);初始化的是单线程套间(Apartment),也就是STA,这样的话,
    你只能在单线程中使用COM对象。::CoInitializeEx(NULL)
    用这个替代试试看
      

  2.   

    在每个MFC线程开始的时候调用AfxOleInit就好了。在OnInitDialog的开始处不需要初始化COM。
    建议尽快关闭连接,以避免占用服务器资源。
    数据库访问代码要加异常处理。
      

  3.   

    你试试清除Debug文件重新编译看看.
      

  4.   

    你不是加了catch么,出错的描述是什么?
      

  5.   

    m_pRecordset=m_pConnection->Execute(bstr,NULL,adCmdText); 
    应该是这句话出现了问题,我以前碰到过类似的问题。改为
    m_pRecordset->Open(bstrSQL,   m_pConnection.GetInterfacePtr(),    adOpenStatic,  adLockOptimistic,  adCmdText);
    试试 这样返回的结果集可以处理。
      

  6.   

    那请问是不是还是按照我现在这个,把代码都放在IDOK里,随时访问随时关闭这样的好?最好不要放在InitInstance里是么?那这样在访问有大量数据的数据库时会不会变慢啊?
      

  7.   

    我给你总结一下:
    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("请确定数据库在同一目录");    }

    }
      

  8.   

    初始化部分 都放在initinstance 或者 放在 initdialog中 
      

  9.   

    我终于知道我哪个地方错了~
    原来还是在打开数据表那个地方!我是这样改的
    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教程,所以大家麻烦说的详细点啊!多谢大家的帮忙啊!谢谢,谢谢!
      

  10.   

    extern CWwwApp theApp谁告诉你要这样的?要实现你的效果,直接写到InitDialog中不就行了