vc+ado调试问题。

rocklabzhang 2005-08-24 09:57:08
以下是我程序代码及其步骤:(工程名:MyADO)
1、在StdAfx.h中,#import "C:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename("EOF","adoEOF")
2、在MyADO.h中加入: _ConnectionPtr m_pConnection;
3、在MyADO.cpp中的InitInstance()中加入:
::AfxOleInit();

HRESULT hr;
hr=m_pConnection.CreateInstance(__uuidof(Connection));
try
{
m_pConnection->ConnectionString="Provider=SQLOLEDB.1;Password=rockwell;Persist Security Info=True;User ID=sa;Initial Catalog=Exa";
// m_pConnection->Open("","","",adConnectUnspecified);
m_pConnection->Open("","","",adModeUnknown);
}
catch(_com_error e)
{
AfxMessageBox("数据库连接失败,确认数据库在当前路径下!");
return FALSE;
}4、在MyADODlg.h中加入变量: _RecordsetPtr m_pRecordset;
5、在MyADODlg.cpp的OnInitDialog()中加入:m_pRecordset.CreateInstance(__uuidof(Recordset));
在一个按钮的消息响应函数中的代码:
CMyADOApp * ptheApp=((CMyADOApp *)AfxGetApp());
try
{
//m_pRecordset->Open("select * from Exat",((CMyADOApp *)AfxGetApp())->m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
m_pRecordset->Open("select * from Exat",ptheApp->m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
}


while(!m_pRecordset->adoEOF)
{
//

((CListBox *)GetDlgItem(IDC_LST))->AddString((_bstr_t)m_pRecordset->GetCollect("年份"));
m_pRecordset->MoveNext();
}
//((CMyADOApp *)AfxGetApp())->m_pConnection->Close();
ptheApp->m_pConnection->Close();
m_pRecordset->Close();
//((CMyADOApp *)AfxGetApp())->m_pConnection->Release();
ptheApp->m_pConnection->Release();
m_pRecordset->Release();


自己感觉没有什么问题,链接也没有错误,但在执行时有错误。
提示:“0x00403326”指令引用的“0x00000000”内存,该内存不能为“read”
...全文
117 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
handsomerun 2005-08-25
  • 打赏
  • 举报
回复
Initinstance先的,呵呵

rocklabzhang 2005-08-25
  • 打赏
  • 举报
回复
现在检查出m_pRecordset->Open("select * from Exat",ptheApp->m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);中的ptheApp->m_pConnection.GetInterfacePtr()指针为空,是不是m_pConnection没有实例化?
但是我在Initinstance()中实例化了的,我的m_pRecordset是在OnInitDilog()中实例化的,冒昧问一下,Initinstance()和OnInitDilog()谁先执行?
skyfine 2005-08-25
  • 打赏
  • 举报
回复
我觉得你先把这些东西写在一个函数里。然后再分开的好。

分开后能确保必须的顺序。也要注意对象在函数之间传递中是有效的
rocklabzhang 2005-08-25
  • 打赏
  • 举报
回复
我现在把m_pConnection.CreateInstance(__uuidof(Connection)).....等语句放在dlg实例化前面解决了指针空的问题。
但是调试到在(CListBox *)GetDlgItem(IDC_LST))->AddString((_bstr_t)m_pRecordset->GetCollect("years"))时又出现问题了,单步执行到inline _variant_t::_variant_t(VARIANT& varSrc, bool fCopy) throw(_com_error)时跳出一个对话框提示:Find Source
Please enter the path for MEMCPY.ASM,选择OK又说在该目录下没有文件。

不知道哪位大哥遇到过我这种情况没?
rocklabzhang 2005-08-24
  • 打赏
  • 举报
回复
调试运行到这个函数的时候出错,
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();
}
}

提示:Unhandled exception in MyADO.exe:0xC0000005:Access Violation

我感觉是ptheApp->m_pConnection.GetInterfacePtr()这个指针的问题。
但不知道怎么解决。
handsomerun 2005-08-24
  • 打赏
  • 举报
回复
跟踪一下吧,在按钮的消息响应函数跟踪看一下,到哪出错了

4,011

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 数据库
社区管理员
  • 数据库
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧