一个简单的ADO有关问题,求帮忙,多谢了
一个简单的ADO问题,急求帮忙,谢谢了
利用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。
各位大神帮我分析看看,哪里写的有问题,着急要用,大家帮帮忙,谢谢了。
------解决方案--------------------
"select TOP 1 from tab_info_config" 这个有问题 应该是top 1 *
利用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。
各位大神帮我分析看看,哪里写的有问题,着急要用,大家帮帮忙,谢谢了。
------解决方案--------------------
"select TOP 1 from tab_info_config" 这个有问题 应该是top 1 *