C# SQL异常关键字';和';

C# SQL异常关键字';和';,c#,sql,asp.net,sql-server,C#,Sql,Asp.net,Sql Server,我正在创建一个asp web表单页面,这部分正在构建一个查询,当添加boatYear部分时,会引发一个异常。在数据库中,它是一个smallint System.Data.dll中发生“System.Data.SqlClient.SqlException”类型的异常,但未在用户代码中处理 其他信息:关键字“AND”附近的语法不正确 查询: string qs = Request.QueryString["dir"].ToString(); string sql = "Select

我正在创建一个asp web表单页面,这部分正在构建一个查询,当添加
boatYear
部分时,会引发一个异常。在数据库中,它是一个
smallint

System.Data.dll中发生“System.Data.SqlClient.SqlException”类型的异常,但未在用户代码中处理

其他信息:关键字“AND”附近的语法不正确

查询:

    string qs = Request.QueryString["dir"].ToString();
    string sql = "Select * From Boats ";
    string boatClass = "";
    string boatYear = "";
    string boatMake = "";
    string boatUsedNew = "";

    if (qs.Equals("f"))
    {
        boatClass = (string)Session["class"];
        boatYear = (string)Session["year"];
        boatMake = (string)Session["make"]; ;
        boatUsedNew = (string)Session["usednew"];
    }

    string where = "";

    if (qs != "b")
    {
        if (boatClass != "all" && boatClass != "")
        {
            where = "Where Class = '" + boatClass + "'";
        }

        if (boatYear != "all" && boatYear != "")
        {
            if (where == "")
            {
                where += "Where ";
            }
            else
            {
                where += " AND ";
            }
            where += "Year = " + boatYear;
        }

        if (boatMake != "all" && boatMake != "")
        {
            if (where == "")
            {
                where += "Where ";
            }
            else
            {
                where += " AND ";
            }
            where += "Make = '" + boatMake + "'";
        }

        if (boatUsedNew != "all" && boatUsedNew != "")
        {
            if (where == "")
            {
                where += "Where ";
            }
            else
            {
                where += " AND ";
            }
            where += "UsedOrNew = '" + boatUsedNew + "'";
        }

        sql += where;
        Session["sql"] = sql;
    }
    else
    {
        sql = (string)Session["sql"];
    }

正如所指出的,这种方法易于SQL注入。为了保护代码不受SQL注入的影响,应该使用参数化查询,即在条件中放置参数名而不是直接值。编写SQL语句仍然可以使用字符串列表来完成

List<string> conditions = new List<string>();

if (boatClass != "all" && !string.IsNullOrEmpty(boatClass))
  conditions.Add("[Class] = @boatClass");
if (boatYear != "all" && !string.IsNullOrEmpty(boatYear))
  conditions.Add("[Year] = @boatYear");
if (boatMake != "all" && !string.IsNullOrEmpty(boatMake))
  conditions.Add("[Make] = @boatMake");
if (boatUsedNew != "all" && !string.IsNullOrEmpty(boatUsedNew))
  conditions.Add("[UsedOrNew] = @boatUsedNew");

if (conditions.Count > 0)
  sql += " where " + string.Join(" AND ", conditions);
列表条件=新列表();
if(boatClass!=“all”&&!string.IsNullOrEmpty(boatClass))
添加(“[Class]=@boatClass”);
if(boatYear!=“all”&&!string.IsNullOrEmpty(boatYear))
添加(“[年]=@boatYear”);
if(boatMake!=“all”&&!string.IsNullOrEmpty(boatMake))
添加(“[Make]=@boatMake”);
if(boatUsedNew!=“all”&&!string.IsNullOrEmpty(boatUsedNew))
添加(“[UsedOrNew]=@boatUsedNew”);
如果(conditions.Count>0)
sql+=“其中”+string.Join(“和”,条件);

当然,应该将正确类型的参数传递给使用此SQL设置和运行命令的代码。

是否尝试打印SQL?这是一种非常糟糕的做法,容易出错。这很难理解…您应该简单地调试生成的sql。如果你的情况很奇怪。你知道你可以使用string.IsNullOrEmpty()对吗?啊。为什么不使用
,其中1=1
,那么您只需始终使用
添加新子句即可。这将大大降低您的代码的意大利面…您可能在SQL字符串中缺少一些空格。我仍然会得到相同的异常快乐SQL注入party@Igor您的答案是正确的,但您应该为这样一个事实添加一些警告,即以这种方式连接字符串容易被黑客攻击。这是一个私人项目,它不在任何服务器上运行,我只需要基本的工作first@cb01Stackoverflow答案不仅仅针对提问者