【问题标题】:Nested If statement with Conditional "and" in SQLSQL中带有条件“and”的嵌套If语句
【发布时间】:2021-04-01 21:39:15
【问题描述】:

我有这行代码

string sqlQuery = "SELECT studentid,StudentName,age,contact FROM tblStudent2 ";

if (txtStudentName.Text != "" && txtStudentId.Text != "" && txtAge.Text != "" && txtContact.Text != "")
{
    sqlQuery += " Where ";

    if (txtStudentName.Text != "")
    {
        sqlQuery += "studentId = '" + txtStudentName.Text + "'";
    }
    sqlQuery += " and ";

    if (txtStudentId.Text != "")
    {
        sqlQuery += "studentId = '" + txtStudentId.Text + "'";
    }
    sqlQuery += " and ";

    if (txtAge.Text != "")
    {
        sqlQuery += "age ='" + txtAge.Text + "'";
    }

    sqlQuery += " and ";

    if (txtContact.Text != "")
    {
        sqlQuery += " FROM tblStudent2 Where contact ='" + txtContact.Text + "'";
    }
}

我的问题目标是从表中选择 (tblStudent2) 。有了这个 SQL 语句

"SELECT studentid,StudentName,age,contact FROM tblStudent2 " + "and (TableColumns) and (TableColumns)""

但目标是在嵌套的 IF 参数之间添加“and”,如果我将其添加到 if 语句中,如果我追加,结果 sqlQuery 将导致“and”作为句子的最后一个单词“和”字。

【问题讨论】:

  • 连接 SQL 是灾难的根源。不要这样做。使用参数化查询,以免成为Little Bobby Tables 的受害者。 Stack Overfow 和 Oracle 的网站上有大量关于它的信息。
  • 我有一个单独的函数来解析这个查询。我的单独函数已经过测试,我猜它对 sql 注入具有弹性。我了解sql注入的危险。谢谢你警告我。

标签: c# mysql sql


【解决方案1】:

我建议在不使用布尔值的情况下编写此逻辑:

string sqlQuery = "SELECT studentid,StudentName,age,contact FROM tblStudent2";
string sqlWhere = ""
if (txtStudentName.Text != "")
    {
        sqlWhere += "StudentName = '" + txtStudentName.Text + "' and ";
    }
if (txtStudentId.Text != "")
    {
        sqlWhere += "studentId = '" + txtStudentId.Text + "' and ";
    }
if (txtAge.Text != "")
    {
        sqlWhere += "age ='" + txtAge.Text + "' and ";
    }
if (txtContact.Text != "")
    {
        sqlWhere += "contact ='" + txtContact.Text + "' and ";
    }
if (sqlWhere != "") {
    sqlQuery += " WHERE " + sqlWhere.Substring(0, myString.Length-5);
}

也就是说,您的代码存在重大问题。您正在使用用户输入值修改查询字符串——由于 SQL 注入和意外(且难以调试)语法错误。这是非常危险的。您应该参数化查询,所以它看起来更像:

if (txtStudentName.Text != "")
    {
        sqlWhere += "StudentName = @StudentName and "
    }

(等等)。

然后在执行时将参数传递给查询。

【讨论】:

    【解决方案2】:

    最好在 if 条件中的每个语句末尾附加“AND”,并删除最后一个,如下所示:

    string sqlQuery = "SELECT studentid,StudentName,age,contact FROM tblStudent2";
    bool flag = false;
    if (txtStudentName.Text != "")
        {
            sqlQuery += "StudentName = '" + txtStudentName.Text + "' and ";
            flag = true;
        }
    if (txtStudentId.Text != "")
        {
            sqlQuery += "studentId = '" + txtStudentId.Text + "' and ";
            flag = true;
        }
    if (txtAge.Text != "")
        {
            sqlQuery += "age ='" + txtAge.Text + "' and ";
            flag  = true;
        }
    if (txtContact.Text != "")
        {
            sqlQuery += "contact ='" + txtContact.Text + "' and ";
            flag = true;
        }
    if (flag == true){
        sqlQuery += " WHERE " + sqlQuery.Substring(0, myString.Length-5);
    }
    

    【讨论】:

    • 谢谢,我认为这段代码可以完美运行。我正在挠头让它工作。我学到了一些新命令,尤其是对编辑代码有很大帮助的子字符串。
    • 或以WHERE 1=1 开头,然后在需要的地方附加每个条件:+ " AND column=value"
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-03
    相关资源
    最近更新 更多