vc+ado调试问题。
以下是我程序代码及其步骤:(工程名: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”