where语句 三个条件

来源:百度知道 编辑:UC知道 时间:2024/06/26 06:25:27
string sql = "select count(*) from tLogin where userName='" + userName + "' userPwd='" + userPwd+"' and userIdentity=" + userIdentity;
编译时总提示我int ret = (int)cmd.ExecuteScalar(); 等号附近有错误
是我的SQL语句错误吗? where后接三个条件 我写的对吗?

PK说的对,少了一个AND,建议你改写如下:
string sql = @"
SELECT COUNT(*)
FROM tLogin
WHERE (userName = '" + userName + @"')
AND (userPwd = '" + userPwd + @"')
AND (userIdentity = " + userIdentity + ")";
不过我更建议你把3个参数改成参数化输入,否则很容易被注入。

这样写:
string sql = @"select count(*) from tLogin where "+
"userName='" + erName + "' "+
" and userPwd='" + userPwd+"' "+
"and userIdentity= " + userIdentity;

userIdentity这一列是int类型的吗?不是的话要加''。
这样分层写好就行了。
你单步运行,直接看sql语句很容易的,然后把sql语句在数据库中运行下,看有结果没,就知道sql语句是否错误了。

+ userName + "' userPwd='" 好像少了一个and

and 少了。
string sql = "select count(*) from tLogin where userName='" + userName + "' and userPwd='" + userPwd+"' and userIdentity=" + userIdentity;