求助关于Sql语句问题 本帖最后由 oyljerry 于 2011-05-03 17:09:21 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 select * from 表明 where 条件获取的记录集不为空,就代表有数据 ADO访问数据库的基本步骤:(1)、引入ADO类 ,加到stdafx.h中#endif // _AFX_NO_AFXCMN_SUPPORT后面 #import "c:\program files\common files\system\ado\msado15.dll" \ no_namespace \ rename ("EOF", "adoEOF") (2)、初始化COM (放在对话框初始化函数里面或者CXXXApp::InitInstance()中) 在MFC中可以用AfxOleInit();非MFC环境中用: CoInitialize(NULL); (3)、包含后就可以用3个智能指针了:_ConnectionPtr、_RecordsetPtr和_CommandPtr (4)、连接数据库HRESULT hr; try { hr = m_pConnection.CreateInstance("ADODB.Connection");///创建Connection对象 if(SUCCEEDED(hr)) { m_pConnection->ConnectionTimeout = 0; hr = m_pConnection->Open( "Provider=SQLOLEDB.1;Password=密码;Persist Security Info=True;User ID=用户名;Initial Catalog=数据库名;Data Source=127.0.0.1(本机)", "", "", adConnectUnspecified); m_pCommand.CreateInstance(__uuidof(Command)); m_pCommand->CommandTimeout = 5; m_pCommand->ActiveConnection = m_pConnection; } } catch(_com_error e)///捕捉异常 { CString errormessage; errormessage.Format("连接数据库失败!\r\n错误信息:%s,%s",e.ErrorMessage(),e.Description()); AfxMessageBox(errormessage);///显示错误信息 } (5)、打开记录集首先创建一个_RecordsetPtr实例,然后调用Open()得到一条SQL语句的执行结果 _RecordsetPtr m_pRecordset; m_pRecordset.CreateInstance(__uuidof(Recordset)); // 在ADO操作中建议语句中要常用try...catch()来捕获错误信息, // 因为它有时会经常出现一些意想不到的错误try { m_pRecordset->Open("SELECT * FROM DemoTable",// 查询DemoTable表中所有字段 m_pConnection.GetInterfacePtr(), // 获取库接库的IDispatch指针 adOpenDynamic, adLockOptimistic, adCmdText); } catch(_com_error *e) { AfxMessageBox(e->ErrorMessage()); } …… 看结果集内容如果结果集有内容就是有你想要的,否则就没有,比如:_RecordsetPtr m_pRecordset; m_pRecordset.CreateInstance(__uuidof(Recordset)); HRESULT hr;hr=m_pRecordset->Open("SELECT * FROM TableName",//TableName替换为你的表名 m_pConnection.GetInterfacePtr(), adOpenDynamic, adLockOptimistic, adCmdText);if(hr)AfxMessageBox(_T("有数"));elseAfxMessageBox(_T("没有数")); picture控件的问题? unknown error Ox800A0BCD 求帮组 对话框之间值传递 竟然有这样翻译中文...暴笑. 请教CFileDialog::GetNextPathName用法 CEdit密码的截取问题 如何在自己写的类中屏蔽CEdit的消息?! 如何用程序代码改变与对话框控件绑定的变量的值? VC6.0 有那些地方跟ISO C++不兼容的地方 ======在背单词程序中,怎样从词库中一个一个的取词?======== vc调用带两个参数的sql存储过程 一个生产者和多个消费者
获取的记录集不为空,就代表有数据
#import "c:\program files\common files\system\ado\msado15.dll" \
no_namespace \
rename ("EOF", "adoEOF") (2)、初始化COM (放在对话框初始化函数里面或者CXXXApp::InitInstance()中)
在MFC中可以用AfxOleInit();非MFC环境中用:
CoInitialize(NULL);
(3)、包含后就可以用3个智能指针了:_ConnectionPtr、_RecordsetPtr和_CommandPtr (4)、连接数据库
HRESULT hr;
try
{
hr = m_pConnection.CreateInstance("ADODB.Connection");///创建Connection对象
if(SUCCEEDED(hr))
{
m_pConnection->ConnectionTimeout = 0;
hr = m_pConnection->Open( "Provider=SQLOLEDB.1;Password=密码;Persist Security Info=True;User ID=用户名;Initial Catalog=数据库名;Data Source=127.0.0.1(本机)", "", "", adConnectUnspecified);
m_pCommand.CreateInstance(__uuidof(Command));
m_pCommand->CommandTimeout = 5;
m_pCommand->ActiveConnection = m_pConnection;
}
}
catch(_com_error e)///捕捉异常
{
CString errormessage;
errormessage.Format("连接数据库失败!\r\n错误信息:%s,%s",e.ErrorMessage(),e.Description());
AfxMessageBox(errormessage);///显示错误信息
}
(5)、打开记录集
首先创建一个_RecordsetPtr实例,然后调用Open()得到一条SQL语句的执行结果
_RecordsetPtr m_pRecordset;
m_pRecordset.CreateInstance(__uuidof(Recordset));
// 在ADO操作中建议语句中要常用try...catch()来捕获错误信息,
// 因为它有时会经常出现一些意想不到的错误
try
{
m_pRecordset->Open("SELECT * FROM DemoTable",// 查询DemoTable表中所有字段
m_pConnection.GetInterfacePtr(), // 获取库接库的IDispatch指针
adOpenDynamic,
adLockOptimistic,
adCmdText);
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
} ……
如果结果集有内容就是有你想要的,否则就没有,比如:_RecordsetPtr m_pRecordset;
m_pRecordset.CreateInstance(__uuidof(Recordset));
HRESULT hr;
hr=m_pRecordset->Open("SELECT * FROM TableName",//TableName替换为你的表名
m_pConnection.GetInterfacePtr(),
adOpenDynamic,
adLockOptimistic,
adCmdText);
if(hr)
AfxMessageBox(_T("有数"));
else
AfxMessageBox(_T("没有数"));