VC中使用SQL语句的时候如何使用变量?高分!

hardstudylulin 2004-11-30 10:58:04
在VC中使用ADO 进行数据库查询等操作,使用SQL语句的时候如何使用变量。

例如:
m_pRecordset.CreateInstance(__uuidof(Recordset));
m_pRecordset->Open("SELECT * FROM Operator_Table WHERE Level= 'A%'
theApp.m_pConnection.GetInterfacePtr(),
adOpenDynamic,adLockOptimistic,adCmdText);
中的语句“SELECT * FROM Operator_Table WHERE Level= 'A%'”
我想用一个变量CString strValue;来代替‘A%’。
也就是类似这样:“SELECT * FROM Operator_Table WHERE Level= strValue”
请问如何表达?
急疯了!谢谢各位!

...全文
221 点赞 收藏 24
写回复
24 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
社会栋梁 2004-12-01
“SELECT * FROM Operator_Table WHERE Level= 'A%'”
str.Format(“SELECT * FROM Operator_Table WHERE Level= 'A%%'”)
要%%来表示%
回复
shenke410 2004-12-01
注意 = 和 LIKE 的区别!!!
回复
zephyr518 2004-12-01
int nYear,nMonth,nDay;
nYear = atoi(strtok(strValue.GetBuffer(strValue.GetLength()),"-"));
nMonth = atoi(strtok(NULL,"-"));
nDay = atoi(strtok(NULL,"-"));
strValue.ReleaseBuffer();
strSql.Format("SELECT * FROM Budget where Year(DateA)=%d and Month(DateA)=%d and Day(DateA)=%d",nYear,nMonth,nDay);
pTodayRes->Open(_variant_t(strSql),m_pConnection.GetInterfacePtr(), adOpenDynamic,adLockOptimistic,adCmdText);

回复
zephyr518 2004-12-01
int nYear,nMonth,nDay;
nYear = atoi(strtok(strValue.GetBuffer(strValue.GetLength()),"-"));
nMonth = atoi(strtok(NULL,"-"));
nDay = atoi(strtok(NULL,"-"));
strValue.ReleaseBuffer();
strSql.Format("SELECT * FROM Budget where Year(DateA)=%d and Month(DateA)=%d and Day(DateA)=%d",nYear,nMonth,nDay);
pTodayRes->Open(_variant_t(strSql),m_pConnection.GetInterfacePtr(), adOpenDynamic,adLockOptimistic,adCmdText);

回复
Mr-Chen 2004-12-01

cstring sqlcommand;
st = "SELECT * FROM Operator_Table WHERE Level= ";
sqlcommand.format("s%s%",st,strValue);
回复
oyljerry 2004-12-01
CString的Format中%s引入你的字符串变量
回复
lhzhang1381 2004-12-01
我顶一下吧!
回复
lg2001 2004-12-01
接分,全让楼上的给说了
回复
aoosang 2004-12-01
其实蛮简单的,楼上的把答案都说了,开始接分了
回复
loki2k 2004-12-01
用CString 的Format函数
如:
CString value("");
CString sqlstr("");
value = 查询的值;
sqlstr.Format("select * from ip_alarmlist where neid = '%s'",value);
也可以用 hbxtlhx(下着春雨的天) 说的,最好是用他的方法,安全性高些!
回复
还是用Parameter比较好些.......
回复
_三皮_ 2004-12-01
CString strSQL;
strSQL="SELECT * FROM Operator_Table WHERE Level='"+strValue+"'";
m_pRecordset->Open(strSQL,theApp.m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);

用'"+strValue+"'引用你的变量
回复
yanw0212 2004-12-01
sql = " SELECT DISTINCT a.LogID, a.HouseID, a.IPAddress, a.ControllerID,a.FirstTime ,a.FirstInfo, \
a.SecondTime,a.SecondInfo , w.WorkModeName, c.LogicName ,h.OwnerName, h.OwnerMobilePhone, \
h.ResidentName, h.ResidentMobilePhone, r.RoomName, d.DeviceName ,p.PortNum , p.PortType , \
(SELECT u.UserName FROM t_UserInfo u WHERE a.FirstUserID=u.UserID) as FirstUser,\
(SELECT u.UserName FROM t_UserInfo u WHERE a.SecondUserID=u.UserID) as SecondUser \
FROM t_AlarmLog AS a \
INNER JOIN t_AlarmPortLog AS p ON p.LogID=a.LogID \
INNER JOIN t_WorkMode AS w ON w.WorkModeID=a.WorkModeID \
INNER JOIN t_RoomInfo AS r ON r.HouseID=a.HouseID AND r.RoomID=p.RoomID \
INNER JOIN t_DeviceInfo AS d ON d.DeviceID=p.DeviceID \
INNER JOIN t_HouseInfo AS h ON h.HouseID=a.HouseID \
INNER JOIN t_ControlLogicInfo AS c ON c.LogicID=a.LogicID AND c.HouseID=a.HouseID \
WHERE a.FirstInfo is not NULL AND a.SecondInfo IS NOT NULL AND d.DeviceName

LIKE '%";
sql += m_alarmtype;
sql +="%'";
回复
mynamelj 2004-12-01
CString str;
str.Format("SELECT * FROM Operator_Table WHERE Level = '%s'",strValue);

pRecordset->Open((LPCSTR)str,theApp.m_pConnection.GetInterfacePtr(),
adOpenDynamic,adLockOptimistic,adCmdText);

回复
快乐鹦鹉 2004-12-01
CString strValue;
CString sSql;
sSql.Format("SELECT * FROM Operator_Table WHERE Level= '%s',strValue);
m_pRecordset->Open(_bstr_t(sSql),theApp.m_pConnection.GetInterfacePtr(),
adOpenDynamic,adLockOptimistic,adCmdText);
回复
3ks 2004-12-01

zyleon(zy)的方法不错,先把sql语句format出来,这样代码也比较直观、易读
回复
hardstudylulin 2004-12-01
非常感谢各位!开饷了!
回复
zyleon 2004-11-30
CString str="abc";
CString s;
s.Format("SELECT * FROM Operator_Table WHERE Level= '%s'",str);
回复
flyelf 2004-11-30
SELECT * FROM Operator_Table WHERE Level= '%s'
回复
hardstudylulin 2004-11-30
谢谢:ponydph(poiny)
还有没有更简单的表达方法,例如在VB中可用‘“str”’!
回复
加载更多回复
相关推荐
发帖
VC/MFC
创建于2007-09-28

1.5w+

社区成员

VC/MFC相关问题讨论
申请成为版主
帖子事件
创建了帖子
2004-11-30 10:58
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……