调用函数OpenSql来返回记录集的时候,出现错误:无效的指针!
void CADOConn::CConn(_ConnectionPtr m_pConnection){
::CoInitialize(NULL);//初始化COM
HRESULT hr;
m_pConnection = NULL;
try
{
hr=m_pConnection.CreateInstance(__uuidof(Connection));
if(SUCCEEDED(hr))
{
CString constr;
constr = "Provider=sqloledb;Data Source=196.196.196.58;Initial Catalog=demo;User Id=sa;Password=dbtest321;";
m_pConnection->ConnectionTimeout = 10;
hr = m_pConnection->Open(_bstr_t(constr), "sa", "dbtest321", adModeUnknown);
}
}
catch(_com_error e)
{
CString errormessage;
errormessage.Format("连接数据库失败!\r\n错误信息:%s",e.ErrorMessage());
AfxMessageBox(errormessage);
return;
}
}
_RecordsetPtr CDataset::OpenSql(_RecordsetPtr Record,LPCTSTR strsql)
{
::CoInitialize(NULL);
_ConnectionPtr m_pConnection;
this->CConn(m_pConnection);
//Record = NULL;
Record.CreateInstance(__uuidof(Recordset));
Record->CursorLocation = adUseClient;
try{
HRESULT hr = Record->Open((LPCSTR) strsql,
_variant_t((IDispatch*)m_pConnection,true),
//m_pConnection.GetInterfacePtr(),
//vtMissing,
adOpenStatic,
adLockOptimistic,
adCmdUnknown);//adCmdText
}
catch(_com_error &e)
{
CString errormessage;
errormessage.Format("获取记录集失败!\r\n错误信息:%s",e.ErrorMessage());
AfxMessageBox(errormessage);
}
m_pConnection->Close();
::CoUninitialize();
return Record;
}
我在其它地方调用这个函数OpenSql来返回记录集的时候,出现错误:无效的指针!
不知错误出在哪里!请高手多多帮忙!!其中:CDataset是继承CADOConn的,编译没错,传入的SQL语句也没错:
CString strsql;
strsql = " select * from [reguser] where username='111' and pwd ='222'";