我用ADO+ACCESS,写一个树!我在初使化树叉的函数(ShowTree())中,调用了
m_pRecordset.CreateInstance("ADODB.Recordset");
m_pRecordset->Open("SELECT * FROM NoteTree",_variant_t((IDispatch*)theApp.m_pConnection,true),/*adOpenDynamic*/adOpenStatic,adLockOptimistic,adCmdText);
语句,一都都正常。但当我“单击树叉显示详细信息”这个函数中要再次调用m_pRecordset时,却出错了,在这个函数中必须调用m_pRecordset->Close,然后重新m_pRecordset->Open……
可是一调用m_pRecordset->Close就出错,在单击树叉出错时就显示“RunTime Error”的错误。abnormal program termination!
PS:编译时没有出错!
这是怎么回事儿,请大家帮帮我!

解决方案 »

  1.   

    close是函数,m_pRecordset->Close();你试试
      

  2.   

    楼上的情况是可以编译过去,不过我的测试结果和楼主的错误不太一样
    可以再if(m_pRecordset->state)
    m_pRecordset->Close();
    先探测一下状态
      

  3.   

    使用ADO编程会有些莫名其妙的错误,建议TRY CATCH试试。有时程序中的其他错误也会导致奇怪问题,仔细检查代码!
      

  4.   

    我就是用的Close();我是在这里忘写()了!
    加上if(m_pRecordset->state)还不行!
    我用Try 和Catch试了一下,给出错误信息是“Invalid Pointer”,意思是“无效指针”,对吧!这是怎么回事儿呢!
      

  5.   

    这是我的代码:
    OnSelectTree中的m_pRecordset就说是指针无效,而在ShowTree中的m_pRecordset就没问题!大家看看是怎么回事儿啊! 我要是不解决这个问题,我就完了!!!!555555555~~~void CXTOutBarCtrl1::OnSelectTree(NMHDR* pNMHDR, LRESULT* pResult){
        NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;

        ///获得当前项的句柄,然后你就可以为所欲为了,嘿嘿
        HTREEITEM hTreeItem = ((CMainFrame *)AfxGetMainWnd())->m_wndTreeCtrl.GetSelectedItem ();
    //((CMainFrame *)AfxGetMainWnd())->m_wndTreeCtrl.SelectItem(hTreeItem); ///设定当前项
    CString S1 = ((CMainFrame *)AfxGetMainWnd())->m_wndTreeCtrl.GetItemText(hTreeItem); ///用GetItemText()函数取得选定项的文本:
      

    _variant_t vName;
    CMYPropertyPage cpg; ///获得选中项的文本赋给m_NodeName
    cpg.m_PEdit1=((CMainFrame *)AfxGetMainWnd())->m_wndTreeCtrl.GetItemText(hTreeItem); ///下面演示了用ADO建立一个数据库的记录集的方法。
    ///注意_variant_t,_bstr_t 两个类型和其它类型的转换
    S1="SELECT * FROM NodeTree where NoteText='"+S1+"'";
    //and ParentID ='S2' ";
    try
    {
    // if(m_pRecordset->State)// m_pRecordset->Close(); m_pRecordset->Open((_variant_t)S1,_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
    m_pRecordset->MoveFirst();

    vName=m_pRecordset->GetCollect("NoteText");
    cpg.m_PEdit2=(LPCTSTR)(_bstr_t)vName;
    }
    catch(_com_error e)///捕捉异常
    {
    CString errormessage; errormessage.Format("数据库Error!\r\n错误信息:%s",e.ErrorMessage()); AfxMessageBox(errormessage);///显示错误信息
    } }
        *pResult = 0;
    }
    void CXTOutBarCtrl1::ShowTree()
    {
    try
    {
    m_pRecordset.CreateInstance("ADODB.Recordset");
    m_pRecordset->Open("SELECT * FROM NoteTree",_variant_t((IDispatch*)theApp.m_pConnection,true),/*adOpenDynamic*/adOpenStatic,adLockOptimistic,adCmdText);
    // m_bSuccess = TRUE; //m_pRecordset->MoveLast(); //vNoteID = m_pRecordset->GetCollect("NoteID");
    //NoteCount = vNoteID.lVal; //最大ID号
         HTREEITEM hItem,hSubItem[255]; m_pRecordset->MoveFirst(); ((CMainFrame *)AfxGetMainWnd())->m_wndTreeCtrl.DeleteAllItems(); while(!m_pRecordset->adoEOF)///这里为什么是adoEOF而不是EOF呢?还记得rename("EOF","adoEOF")这一句吗?
    {
    vNoteID = m_pRecordset->GetCollect("NoteID");
    vNoteText = m_pRecordset->GetCollect("NoteText");
    vParentID = m_pRecordset->GetCollect("ParentID");

    //父级项句柄是   hSubItem[vParentID.lVal]
    hItem= hSubItem[vParentID.lVal];
    item=vNoteID.lVal;
    hSubItem[item]=((CMainFrame *)AfxGetMainWnd())->m_wndTreeCtrl.InsertItem((LPCTSTR)(_bstr_t)vNoteText,hItem);
    //设置树叉内含数据
     //((CMainFrame *)AfxGetMainWnd())->m_wndTreeCtrl.SetItemData( hSubItem[item], vParentID.lVal);
    m_pRecordset->MoveNext();///移到下一条记录
    }

    if(m_pRecordset->State) m_pRecordset->Close();
    } catch(_com_error e)///捕捉异常
    {
    CString errormessage; errormessage.Format("读取数据库失败!\r\n错误信息:%s",e.ErrorMessage()); AfxMessageBox(errormessage);///显示错误信息 }
      

  6.   

    ado本来对频繁打开关闭就会出现问题
      

  7.   

    重新初始化一下呢
    或者不用CLOSE 改用null
      

  8.   

    我这个错误不是频繁打开关闭引起的!这个不是Close或不是Close引起的!问题是:m_pRecordset在ShowTree函数中起作好,在OnSelectTree消息映射中就失效了!这是为什么呢?
      

  9.   

    你把m_pRecordset.CreateInstance("ADODB.Recordset");放到构造函数中,看看
      

  10.   

    我把m_pRecordset.CreateInstance("ADODB.Recordset");放到构造函数中以后,在程序初使化时就出错,错误 信息是:Invalid Pointer!原来好使的ShowTree也不行了!大家还有什么好办法吗! 55555~
      

  11.   

    我把m_pRecordset.CreateInstance("ADODB.Recordset");放到构造函数中以后,在程序初使化时就出错,错误 信息是:Invalid Pointer!原来好使的ShowTree也不行了!大家还有什么好办法吗! 55555~
      

  12.   

    将以前创建的对象先释放了
    if(m_pRecordset->State)
    m_pRecordset->Close();
    m_pRecordset.Release();
    m_pRecordset = NULL;
    以后再建m_pRecordset.CreateInstance();
    再使用
      

  13.   

    我照你方法做了,释放了m_pRecordset,然后又重新用m_pRecordset.CreateInstance("ADODB.Recordset");重新创建的!
    但在单击树时,出现如下错误:
    IDispatch error #3127这是怎么回事儿!
      

  14.   

    SunYuan0_1,谢谢你!你的方法解决了我的问题!! :)