我在BOOL CMainWaiterDLG::OnInitDialog()
{
  DisplayDesk();

函数中调用DisplayDesk()函数,用MessageBox("")来跟踪一下,发现弹出一次MessageBox(""),
但是没有弹出第2个MessageBox("a"),就出错了,我找了很久也没有找到原因。
我之前都是用这种方式,来连接数据库的,都没有问题,为什么这次就不行了呢???出错代码如下:
///////////////////********************//////////////////////
     Waiter Interface.exe - 应用程序错误   "0x00402896"指令引用的"0x00000000"内存。该内存不能为"read".
    要终止程序,请单击"确定"
    要调试程序,请单击"取消"////////////////////******************/////////////////////
当我点击取消调试时发现提示器指向如下的代码行:
inline _variant_t::_variant_t(IDispatch* pSrc, bool fAddRef) throw()
{
V_VT(this) = VT_DISPATCH;
V_DISPATCH(this) = pSrc; // Need the AddRef() as VariantClear() calls Release(), unless fAddRef
// false indicates we're taking ownership
//
if (fAddRef) {指向这行有问题=====>>>>>V_DISPATCH(this)->AddRef();/////////// }
}
void CMainWaiterDLG::DisplayDesk()
{  
_RecordsetPtr m_pRecordset;  
CString sql="select TABLENAME,IMAGES from DESCK";     MessageBox("");///////////////(1)步
try
{
m_pRecordset.CreateInstance("ADODB.Recordset");
m_pRecordset->Open((_variant_t)sql,_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
    MessageBox("a");///////////////(2)步
if(!m_pRecordset->adoEOF)
{
m_oList_UseDesk.DeleteAllItems();
}

m_pRecordset->Close();
}
catch(_com_error e)
{
CString stemp;
stemp.Format("读取资料出错:%s",e.ErrorMessage());
AfxMessageBox(stemp);
return;
}
    UpdateData();
}  

解决方案 »

  1.   

    theApp.m_pConnection确保此时连接已成功了吗?
    m_pRecordset.CreateInstance("ADODB.Recordset"); 
    m_pRecordset->Open((_variant_t)sql,_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText); 
    改为下面试试,我一般这么写m_pRecordset.CreateInstance(__uuidof(Recordset)); 
    m_pRecordset->Open((_bstr_t)sql,_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText); 
      

  2.   

    你加一个
    ASSERT(theApp.m_pConnection->GetState() == adStateOpen)
    测试一下。
      

  3.   

    theApp.m_pConnection为空,所以出错,其他代码没问题
      

  4.   


    连接数据库的问题解决了,,,,不过新的问题又来了,请帮一下,,,,我 Clean 一下,,,发现怎么都编译不过去,;;提示错误如下--------------------Configuration: Waiter Interface - Win32 Debug--------------------
    Compiling...
    StdAfx.cpp
    c:\program files\microsoft visual studio\vc98\include\exdisp.h(266) : fatal error C1083: Cannot open include file: 'docobj.h': No such file or directory
    Error executing cl.exe.
    Creating browse info file...
    BSCMAKE: error BK1506 : cannot open file '.\Debug\StdAfx.sbr': No such file or directory
    Error executing bscmake.exe.Waiter Interface.exe - 2 error(s), 0 warning(s)