一个简单的ADO问题,急求帮忙,谢谢了
时间:2011-12-14
来源:互联网
利用VC++2008,MFC,sql2005开发
连接类ADOConn的部分定义如下(里面的AfxMessageBox函数主要用来调试的方便):
void ADOConn::OnInitADOConn()
{
//初始化库环境
::CoInitialize(NULL);
try{
//创建connection对象
m_pConnection.CreateInstance("ADODB.Connection");
//设置链接字符串
_bstr_t strConnect="Provider=SQLOLEDB; Server=tsis_server;Database=pbsystem_test; uid=sunwt; pwd=sunwt2011;";
m_pConnection->Open(strConnect,"","",adModeUnknown);
}
catch(_com_error e)
{
AfxMessageBox(_T("链接出错"));
}
}
void ADOConn::ExitConnect()
{
// 关闭记录集和连接
if (m_pRecordset != NULL)
m_pRecordset->Close();
m_pConnection->Close();
// 释放环境
::CoUninitialize();
}
_RecordsetPtr& ADOConn::GetRecordSet(_bstr_t bstrSQL) // 执行查询
{
try
{
// 连接数据库,如果Connection对象为空,则重新连接数据库
if(m_pConnection==NULL)
OnInitADOConn();
AfxMessageBox(_T("执行查询1"));
// 创建记录集对象
m_pRecordset.CreateInstance(__uuidof(Recordset));
AfxMessageBox(_T("执行查询2"));
// 取得表中的记录
m_pRecordset->Open(bstrSQL,m_pConnection.GetInterfacePtr(),adOpenStatic,adLockReadOnly,adCmdText);
AfxMessageBox(_T("执行查询3"));
}
// 捕捉异常
catch(_com_error e)
{
// 显示错误信息
AfxMessageBox(_T("执行查询出错"));
}
// 返回记录集
return m_pRecordset;
}
// 执行SQL语句
BOOL ADOConn::ExecuteSQL(_bstr_t bstrSQL)
{
_variant_t RecordsAffected;
try
{
// 是否已经连接数据库
if(m_pConnection == NULL)
m_pConnection->Execute(bstrSQL,NULL,adCmdText);
return true;
}
catch(_com_error e)
{
AfxMessageBox(_T("执行SQL语句出错"));
return false;
}
}
==========一个简单的读取如下=================
ADOConn m_AdoConn;
AfxMessageBox(_T("执行查询4"));
m_AdoConn.OnInitADOConn();
AfxMessageBox(_T("执行查询5"));
CString m_sql;
m_sql.Format("select TOP 1 from tab_info_config");
AfxMessageBox(_T("执行查询6"));
_RecordsetPtr m_pRectordset;
m_pRectordset=m_AdoConn.GetRecordSet((_bstr_t)m_sql);
AfxMessageBox(_T("执行查询7"));
if(m_AdoConn.m_pRecordset->adoBOF==true)
{
AfxMessageBox(_T("链接成功"));
}
m_AdoConn.ExitConnect();
===========问题如下===============
运行的时候,这个函数AfxMessageBox(_T("执行查询X"));
显示的顺序依次是:执行查询4、5、6、1、2、执行查询出错、7。
各位大神帮我分析看看,哪里写的有问题,着急要用,大家帮帮忙,谢谢了。
连接类ADOConn的部分定义如下(里面的AfxMessageBox函数主要用来调试的方便):
void ADOConn::OnInitADOConn()
{
//初始化库环境
::CoInitialize(NULL);
try{
//创建connection对象
m_pConnection.CreateInstance("ADODB.Connection");
//设置链接字符串
_bstr_t strConnect="Provider=SQLOLEDB; Server=tsis_server;Database=pbsystem_test; uid=sunwt; pwd=sunwt2011;";
m_pConnection->Open(strConnect,"","",adModeUnknown);
}
catch(_com_error e)
{
AfxMessageBox(_T("链接出错"));
}
}
void ADOConn::ExitConnect()
{
// 关闭记录集和连接
if (m_pRecordset != NULL)
m_pRecordset->Close();
m_pConnection->Close();
// 释放环境
::CoUninitialize();
}
_RecordsetPtr& ADOConn::GetRecordSet(_bstr_t bstrSQL) // 执行查询
{
try
{
// 连接数据库,如果Connection对象为空,则重新连接数据库
if(m_pConnection==NULL)
OnInitADOConn();
AfxMessageBox(_T("执行查询1"));
// 创建记录集对象
m_pRecordset.CreateInstance(__uuidof(Recordset));
AfxMessageBox(_T("执行查询2"));
// 取得表中的记录
m_pRecordset->Open(bstrSQL,m_pConnection.GetInterfacePtr(),adOpenStatic,adLockReadOnly,adCmdText);
AfxMessageBox(_T("执行查询3"));
}
// 捕捉异常
catch(_com_error e)
{
// 显示错误信息
AfxMessageBox(_T("执行查询出错"));
}
// 返回记录集
return m_pRecordset;
}
// 执行SQL语句
BOOL ADOConn::ExecuteSQL(_bstr_t bstrSQL)
{
_variant_t RecordsAffected;
try
{
// 是否已经连接数据库
if(m_pConnection == NULL)
m_pConnection->Execute(bstrSQL,NULL,adCmdText);
return true;
}
catch(_com_error e)
{
AfxMessageBox(_T("执行SQL语句出错"));
return false;
}
}
==========一个简单的读取如下=================
ADOConn m_AdoConn;
AfxMessageBox(_T("执行查询4"));
m_AdoConn.OnInitADOConn();
AfxMessageBox(_T("执行查询5"));
CString m_sql;
m_sql.Format("select TOP 1 from tab_info_config");
AfxMessageBox(_T("执行查询6"));
_RecordsetPtr m_pRectordset;
m_pRectordset=m_AdoConn.GetRecordSet((_bstr_t)m_sql);
AfxMessageBox(_T("执行查询7"));
if(m_AdoConn.m_pRecordset->adoBOF==true)
{
AfxMessageBox(_T("链接成功"));
}
m_AdoConn.ExitConnect();
===========问题如下===============
运行的时候,这个函数AfxMessageBox(_T("执行查询X"));
显示的顺序依次是:执行查询4、5、6、1、2、执行查询出错、7。
各位大神帮我分析看看,哪里写的有问题,着急要用,大家帮帮忙,谢谢了。
作者: vtao213344 发布时间: 2011-12-14
"_RecordsetPtr m_pRectordset;
m_pRectordset=m_AdoConn.GetRecordSet((_bstr_t)m_sql);"
这个新定义的记录集指针得初试话下吧,“m_pRecordset.CreateInstance(__uuidof(Recordset));"
m_pRectordset=m_AdoConn.GetRecordSet((_bstr_t)m_sql);"
这个新定义的记录集指针得初试话下吧,“m_pRecordset.CreateInstance(__uuidof(Recordset));"
作者: shn521 发布时间: 2011-12-14
"select TOP 1 from tab_info_config" 这个有问题 应该是top 1 *
作者: shn521 发布时间: 2011-12-14
引用 1 楼 shn521 的回复:
"_RecordsetPtr m_pRectordset;
m_pRectordset=m_AdoConn.GetRecordSet((_bstr_t)m_sql);"
这个新定义的记录集指针得初试话下吧,“m_pRecordset.CreateInstance(__uuidof(Recordset));"
"_RecordsetPtr m_pRectordset;
m_pRectordset=m_AdoConn.GetRecordSet((_bstr_t)m_sql);"
这个新定义的记录集指针得初试话下吧,“m_pRecordset.CreateInstance(__uuidof(Recordset));"
哦,谢谢,这个在函数中_RecordsetPtr& ADOConn::GetRecordSet(_bstr_t bstrSQL) 初始化了。
top 1 *
有可能是这个问题。
作者: vtao213344 发布时间: 2011-12-14
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28