MFC ADOC访问数据库用公共类报空指针异常,而不用公共类正常

先建立了个公共类用于数据库连接查询如下
ADOConn头文件
#pragma once
//导入AOC动态链接库
#import "C:\Program Files\Common Files\System\ado\msado15.dll" no_namespace\
rename("EOF","adoEOF")rename("BOF","adoBOF")

class ADOConn
{
public:
ADOConn(void);
virtual~ADOConn(void);

_ConnectionPtr m_pConnection;   //链接对象指针
_RecordsetPtr m_pRecordset;     //记录集对象指针
_CommandPtr m_pCommand;         //记录
void OnInitADOConn(void);       //连接数据库
void Exitconnect(void);         //关闭记录集并断开数据库连接
_RecordsetPtr& GetRecordSet(_bstr_t bstrSQL);           //执行SQL查询并返回记录集指针
BOOL ExecuteSQL(_bstr_t bstrSQL);                       //执行SQL语句

};

ADOConn源文件

#include "StdAfx.h"
#include "ADOConn.h"
#include "SM2.h"
#include

ADOConn::ADOConn(void)
{
}

ADOConn::~ADOConn(void)
{
}

void ADOConn::OnInitADOConn(void)
{
try
{
m_pConnection.CreateInstance("ADODB.Connection"); //创建连接对象
// _bstr_t strConnect="Provider=MSDASQL;DSN=Supermarket;UID=sa;PWD=123456;";
_bstr_t strConnect ="Provider = SQLOLEDB; Data Source =localhost; Initial Catlog =Supermarket;\
User ID =sa; Password =123456;";
m_pConnection->Open(strConnect,"","",adModeUnknown); //打开数据库
AfxMessageBox(_T("连接成功"));
}
catch(_com_error e)
{
AfxMessageBox(e.Description()); //错误消息
}
}

_RecordsetPtr& ADOConn::GetRecordSet(_bstr_t bstrSQL)
{
//TODO: insert return statement here

try
{
    if(m_pConnection == NULL)           //判断数据库是否连接,如果不连接
          OnInitADOConn();
    m_pRecordset.CreateInstance(__uuidof(Recordset));       //创建记录集对象实例
    m_pRecordset->Open(bstrSQL,m_pConnection.GetInterfacePtr(),
        adOpenDynamic,adLockOptimistic,adCmdText);          //执行SQL到记录集
}
catch(_com_error e)
{
    e.Description();
}
return m_pRecordset;//返回记录集指针

}

BOOL ADOConn::ExecuteSQL(_bstr_t bstrSQL)
{
try
{
if(m_pConnection == NULL) //判断数据库是否连接,如果不连接
OnInitADOConn();
m_pConnection->Execute(bstrSQL, NULL, adCmdText); //执行SQL语句
return true; //返回真
}
catch( _com_error e)
{
e.Description();
return false; //返回假
}
}

void ADOConn::Exitconnect(void)
{
if(m_pRecordset != NULL) //判断记录集是否打开
m_pRecordset->Close(); //断开记录集
m_pConnection->Close(); //断开数据库
}
然后在查询的时候就会报空指针异常是什么情况
ADOConn m_AdoConnSP; //ADOConn对象
m_AdoConnSP.OnInitADOConn(); //连接数据库
CString sqlSP = _T("select * from Supermarket.dbo.tb_SP");
int i = 0;
m_AdoConnSP.m_pRecordset = m_AdoConnSP.GetRecordSet((_bstr_t) sqlSP); //执行SQL语句
while( !m_AdoConnSP.m_pRecordset->adoBOF)
{
m_Grid.InsertItem(i,_T("")); //向列表中添加行
m_Grid.SetItemText(i, 0, (LPCTSTR)(_bstr_t)m_AdoConnSP.m_pRecordset->GetCollect("SPID"));
m_Grid.SetItemText(i, 1, (LPCTSTR)(_bstr_t)m_AdoConnSP.m_pRecordset->GetCollect("SPName"));
m_Grid.SetItemText(i, 2, (LPCTSTR)(_bstr_t)m_AdoConnSP.m_pRecordset->GetCollect("SPClass"));
m_Grid.SetItemText(i, 3, (LPCTSTR)(_bstr_t)m_AdoConnSP.m_pRecordset->GetCollect("SPUnit"));
m_Grid.SetItemText(i, 4, (LPCTSTR)(_bstr_t)m_AdoConnSP.m_pRecordset->GetCollect("SPBarCode"));
m_Grid.SetItemText(i, 5, (LPCTSTR)(_bstr_t)m_AdoConnSP.m_pRecordset->GetCollect("PrimeCost"));
m_Grid.SetItemText(i, 6, (LPCTSTR)(_bstr_t)m_AdoConnSP.m_pRecordset->GetCollect("SellPrice"));
m_Grid.SetItemText(i, 7, (LPCTSTR)(_bstr_t)m_AdoConnSP.m_pRecordset->GetCollect("SPProfit"));
m_AdoConnSP.m_pRecordset->MoveNext();
i++;
}
m_AdoConnSP.Exitconnect();

然而如果不用公共类的方法就不会报错

try
{
m_pConnection.CreateInstance("ADODB.Connection"); //创建连接对象
_bstr_t strConnect ="Provider = SQLOLEDB; Data Source =localhost; Initial Catlog =Supermarket;\
User ID =sa; Password =123456;";
m_pConnection->Open(strConnect,"","",adModeUnknown); //打开数据库
AfxMessageBox(_T("连接成功"));
}
catch(_com_error e)
{
AfxMessageBox(e.Description()); //错误消息
}
//建立查询
CString sql = _T("select * from Supermarket.dbo.tb_SP");
m_pRecordset.CreateInstance(_uuidof(Recordset));
m_pRecordset->Open((_bstr_t)sql,m_pConnection.GetInterfacePtr(),
adOpenDynamic,adLockOptimistic,adCmdText);
//添加到列表框
int i = 0;
while( !m_pRecordset->adoEOF)
{
m_ListSP.InsertItem(0,_T(""));
m_ListSP.SetItemText(i, 0, (LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("SPID"));
m_ListSP.SetItemText(i, 1, (LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("SPName"));
m_ListSP.SetItemText(i, 2, (LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("SPClass"));
m_ListSP.SetItemText(i, 3, (LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("SPUnit"));
m_ListSP.SetItemText(i, 4, (LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("SPBarCode"));
m_ListSP.SetItemText(i, 5, (LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("PrimeCost"));
m_ListSP.SetItemText(i, 6, (LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("SellPrice"));
m_ListSP.SetItemText(i, 7, (LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("SPProfit"));
m_pRecordset->MoveNext();
}
m_pRecordset->Close();

2个回答

数据库服务启动了吗?

先看你的空指针是什么步骤噗产生的?具体错误原因。

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问