mfc ado连接数据库按条件查询的问题

weixin_35837760 2016-08-17 05:46:03
建立了一个基于单文档的mfc程序,使用ado连接access数据库的一个文件 用res-》open(select*fromXXXwhere id=YYY)代码打开数据库,请问YYY可以用变量表示吗,比如我要打开XXX表上面查询id,设置id是一个变量,请问大神们应该怎样表示where后面的变量YYY,代码应该怎样打,谢谢
...全文
592 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
weixin_35837760 2016-08-18
  • 打赏
  • 举报
回复
引用 7 楼 zhao4zhong1 的回复:
我既给了你纠正后的代码例子,也给了你MSDN中关于CString += 运算符重载的原型。
谢谢,但是CString sql="select * from tCourse where id="; 错误C2440 “初始化”: 无法从“const char [32]”转换为“ATL::CStringT<wchar_t,StrTraitMFC_DLL<wchar_t,ATL::ChTraitsCRT<wchar_t>>>”
赵4老师 2016-08-18
  • 打赏
  • 举报
回复
我既给了你纠正后的代码例子,也给了你MSDN中关于CString += 运算符重载的原型。
weixin_35837760 2016-08-18
  • 打赏
  • 举报
回复
引用 5 楼 zhao4zhong1 的回复:
CString strmingcheng="1";
CString sql="select * from tCourse where id=";
rec.CreateInstance("ADODB.Recordset");
sql+=strmingcheng;
pRst->Open(sql, _variant_t((IDispatch*)pCon), adOpenDynamic, adLockUnspecified, adCmdText);

CString::operator += const CString& operator +=( const CString& string ); throw( CMemoryException ); const CString& operator +=( TCHAR ch ); throw( CMemoryException ); const CString& operator +=( LPCTSTR lpsz ); throw( CMemoryException ); Parameters string A CString to concatenate to this string. ch A character to concatenate to this string. lpsz A pointer to a null-terminated string to concatenate to this string. Remarks The += concatenation operator joins characters to the end of this string. The operator accepts another CString object, a character pointer, or a single character. You should be aware that memory exceptions may occur whenever you use this concatenation operator because new storage may be allocated for characters added to this CString object. Example The following example demonstrates the use of CString::operator +=. // example for CString::operator += CString s( "abc" ); ASSERT( ( s += "def" ) == "abcdef" ); CString Overview | Class Members | Hierarchy Chart See Also CString::operator +
老师,我是一个小白,刚接触MFC,然后这些英文有点难看懂,能给点例子或者应该怎样修改,万分感谢
赵4老师 2016-08-18
  • 打赏
  • 举报
回复
CString strmingcheng="1";
CString sql="select * from tCourse where id=";
rec.CreateInstance("ADODB.Recordset");
sql+=strmingcheng;
pRst->Open(sql, _variant_t((IDispatch*)pCon), adOpenDynamic, adLockUnspecified, adCmdText);

CString::operator += const CString& operator +=( const CString& string ); throw( CMemoryException ); const CString& operator +=( TCHAR ch ); throw( CMemoryException ); const CString& operator +=( LPCTSTR lpsz ); throw( CMemoryException ); Parameters string A CString to concatenate to this string. ch A character to concatenate to this string. lpsz A pointer to a null-terminated string to concatenate to this string. Remarks The += concatenation operator joins characters to the end of this string. The operator accepts another CString object, a character pointer, or a single character. You should be aware that memory exceptions may occur whenever you use this concatenation operator because new storage may be allocated for characters added to this CString object. Example The following example demonstrates the use of CString::operator +=. // example for CString::operator += CString s( "abc" ); ASSERT( ( s += "def" ) == "abcdef" ); CString Overview | Class Members | Hierarchy Chart See Also CString::operator +
weixin_35837760 2016-08-18
  • 打赏
  • 举报
回复
引用 3 楼 zhao4zhong1 的回复:
CString strmingcheng="1";
CString sql;
rec.CreateInstance("ADODB.Recordset");
sql="select * from tCourse where id="+strmingcheng;
pRst->Open(sql, _variant_t((IDispatch*)pCon), adOpenDynamic, adLockUnspecified, adCmdText);
你好老师,我刚按着你的输入进去,sql="select * from tCourse where id="+strmingcheng;这一句报错“没有与操作数匹配的 +运算符”; 然后pRst->Open(sql, _variant_t((IDispatch*)pCon), adOpenDynamic, adLockUnspecified, adCmdText);,sql那里报错“不存在用户定义的“CString”到“const _variant_t”的适当转换”。 错误C2678 二进制“+”: 没有找到接受“const char [32]”类型的左操作数的运算符(或没有可接受的转换) 错误C2664 “HRESULT Recordset15::Open(const _variant_t &,const _variant_t &,CursorTypeEnum,LockTypeEnum,long)”: 无法将参数 1 从“CString”转换为“const _variant_t &”
赵4老师 2016-08-18
  • 打赏
  • 举报
回复
CString strmingcheng="1";
CString sql;
rec.CreateInstance("ADODB.Recordset");
sql="select * from tCourse where id="+strmingcheng;
pRst->Open(sql, _variant_t((IDispatch*)pCon), adOpenDynamic, adLockUnspecified, adCmdText);
weixin_35837760 2016-08-18
  • 打赏
  • 举报
回复
VS2015 MFC基于单文档的程序 定义了一个CString strmingcheng; strmingcheng随着其他因素变化而变化 想要用ADO链接数据库查询数据库的某一条记录 意思就是在tCourse上的id字段查找等于strmingcheng的记录 思考了一下感觉是通过select 语句的where上修改 rec.CreateInstance("ADODB.Recordset"); pRst->Open(“select*from tCourse where id= strmingcheng ”, _variant_t((IDispatch*)pCon), adOpenDynamic, adLockUnspecified, adCmdText); “select*from tCourse where id= strmingcheng ” 问题就是strmingcheng是一个变量,我应该如何修改where后面的代码,也就是说怎样在WHERE后面加变量 求大神们给出应该怎样做 谢谢大神们
赵4老师 2016-08-18
  • 打赏
  • 举报
回复
CString sql=_T("select * from tCourse where id=");
eden922 2016-08-17
  • 打赏
  • 举报
回复
YYY 可以用变量表示 定义一个字符串变量,作为查询语句就可以 CString strSQL; strSQL.Format("select * from XXX where id=%d",YYY); res-》open(strSQL);

4,011

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 数据库
社区管理员
  • 数据库
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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