调用函数OpenSql来返回记录集的时候,出现错误:无效的指针!

gzkelin 2007-10-11 03:39:42


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'";
...全文
286 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
UltraBejing 2008-04-30
  • 打赏
  • 举报
回复
不明LZ在说什么
gzkelin 2007-10-11
  • 打赏
  • 举报
回复
ckt1120
那应该怎么改那个函数呢?
我想这样编程调用那函数OpenSql:
_RecordsetPtr Records;
this->m_pRecordSet.OpenSql(Records,strsql);
来返回Records,接着对Records进行操作.
ckt 2007-10-11
  • 打赏
  • 举报
回复
你在函数里::CoInitialize(NULL);//初始化COM
函数结束就::CoUninitialize();

你在使用函数返回的_RecordsetPtr(已经::CoUninitialize())不会有问题么?
gzkelin 2007-10-11
  • 打赏
  • 举报
回复
ckt1120
谢谢你的回答,你提供的资料我尝试用过,但出现很多的BUG(连这句if(m_pConnection==NULL)都出现BUG,好晕)!
只好参考别人的,然后自己再写些通用的函数.
ckt 2007-10-11
  • 打赏
  • 举报
回复
给你个资料,你参考下


1.为了方面在Visual C++中使用ADO访问数据库,最好进行适当的封装.
多个表时,可以创建多个类来对应,类的成员变量对应表的列,类的成员函数是对
成员变量和表的操作,这种封装比较符合Visual C++的程序设计思想.

2.创建好项目,添加一个新ADOCtrl,在类头文件中加入:
#import "c:\program files\common files\system\ado\msado15.dll"\
no_namespace rename("EOF","adoEOF") rename("BOF","adoBOF")
class ADOCtrl
{
public:
ADOCtrl(void);
~ADOCtrl(void);
public:
void OnInitConnect();//初始化-连接数据库
_RecordsetPtr& GetRecordset(_bstr_t bstrSQL);//执行查询,返回一个记录集
bool ExecuteSQL(_bstr_t bstrSQL);//执行SQL语句,Insert Update
void DisConnect();//断开连接
_ConnectionPtr m_pConnection;// 连接数据库指针
_RecordsetPtr m_pRecordset; //记录集指针 };
具体实现:
ADOCtrl::ADOCtrl(void)
{
}

ADOCtrl::~ADOCtrl(void)
{
}

void ADOCtrl::OnInitConnect()//初始化-连接数据库
{
::CoInitialize(NULL);//初始化OLE/COM环境
try
{
m_pConnection.CreateInstance("ADODB.Connection");//创建Connection对象
_bstr_t strConnect="Provider=Microsoft.Jet.OLEDB.4.0;\
Data Source=E:\\Documents and Settings\\影碟数据库.mdb;";
//设置连接字符串
m_pConnection->Open(strConnect,"","",adModeUnknown);//连接数据库
}
catch(_com_error e)
{
CString error;
error.Format("数据库连接失败:\n%s",e.Description());
AfxMessageBox(error);//显示错误信息
}
}
_RecordsetPtr& ADOCtrl::GetRecordset(_bstr_t bstrSQL)//执行查询,返回记录集
{
try
{
if(m_pConnection==NULL)//如果Connect对象为空,则重新连接数据库
OnInitConnect();
m_pRecordset.CreateInstance(__uuidof(Recordset));//创建记录集对象
m_pRecordset->Open(bstrSQL,m_pConnection.GetInterfacePtr(),adOpenDynamic,
adLockOptimistic,adCmdText);//取得表中的记录
}

catch(_com_error e)
{
AfxMessageBox("记录集获取失败");
}
return m_pRecordset;
}
bool ADOCtrl::ExecuteSQL(_bstr_t bstrSQL)//执行SQL语句,Insert Update
{
_variant_t RecordsAffected;
try
{
if(m_pConnection==NULL)//检查是否连接数据库
OnInitConnect();
m_pConnection->Execute(bstrSQL,NULL,adCmdText);
return true;
}
catch(_com_error e)
{
AfxMessageBox("SQL语句执行失败");
}
}
void ADOCtrl::DisConnect()//关闭记录集,断开连接
{
if(m_pRecordset!=NULL)
m_pRecordset->Close();
m_pConnection->Close();
::CoUninitialize();
}
gzkelin 2007-10-11
  • 打赏
  • 举报
回复
怎么没人回答的?
先顶一下.
gzkelin 2007-10-11
  • 打赏
  • 举报
回复
我上面这种编程思维是否正确呢?我是仿照VB的思维编写的.
请大家多多指明道路.

24,854

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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