c#和sql关键字';和';

c#和sql关键字';和';,c#,sql,sql-server,if-statement,C#,Sql,Sql Server,If Statement,我试图在我的sql语句中找到语法错误,但我无法尝试添加()和[],但没有任何更改,因此请帮助我解决此错误,因为我收到以下错误消息:“关键字‘和’附近的语法不正确”旁边是:“ad.Fill(cdt);” 第二个代码是 protected void FilterBtn_Click(object sender, EventArgs e) { HttpCookie cookie = Request.Cookies.Get("Location"); using (

我试图在我的sql语句中找到语法错误,但我无法尝试添加()和[],但没有任何更改,因此请帮助我解决此错误,因为我收到以下错误消息:“关键字‘和’附近的语法不正确”旁边是:“ad.Fill(cdt);”

第二个代码是

 protected void FilterBtn_Click(object sender, EventArgs e)
    {
        HttpCookie cookie = Request.Cookies.Get("Location");
        using (SqlConnection carcon = new SqlConnection(ConfigurationManager.ConnectionStrings["BeravaConnectionString"].ConnectionString))

            if (cookie != null)
            {
                string sql = @"SELECT [JobNum], [Dept], [PubDate], [EndDate], [Employer],[VacCountry], [VacState], [VacCity],
            [Jobtitle], [CompLogo], SUBSTRING([jobdesc],1,40) as jobdesc FROM [jobs] 
            Where [VacCountry] = @Location AND
            (@State IS NULL OR VacState = @State) AND
            (@City IS NULL OR VacCity = @City)                
            ORDER BY [PubDate] DESC ";

                DataTable cdt = new DataTable();
                SqlCommand ccmd = new SqlCommand(sql, carcon);
                var Location = cookie.Value;
                ccmd.Parameters.AddWithValue("@Location", Location);
                ccmd.Parameters.AddWithValue("@State", filterstathpjob.SelectedValue);
                ccmd.Parameters.AddWithValue("@City", filterJobhpjob.SelectedValue);

                SqlDataAdapter ad = new SqlDataAdapter(ccmd);
                ad.Fill(cdt);
                Joblistview.DataSource = cdt;
                Joblistview.DataBind();

            }
    }

您的第一个代码段中的这一行似乎是您的问题:

ccmd.CommandText = CarSqlST + condition;
您在SQL语句的末尾添加了一个条件,这是在您已经完成了一个
orderby
之后。您的条件需要在订购人之前添加,请尝试此操作

    string CarSqlST = @"SELECT [JobNum], [Dept], [PubDate], [EndDate], [Employer],[VacCountry], [VacState], [VacCity],
    [Jobtitle], [CompLogo], 
    SUBSTRING([jobdesc],1,40) 
    as jobdesc FROM [jobs] Where 1=1 and [VacCountry] = [@Location] ";

    ................


ccmd.CommandText = CarSqlST + condition+" ORDER BY [PubDate] DESC " ;
                SqlDataAdapter ad = new SqlDataAdapter();
                ad.SelectCommand = ccmd;

                ad.Fill(cdt);
                Joblistview.DataSource = cdt;
                Joblistview.DataBind();

            }

条件+=
发生后,您是否尝试过单步查看您的最终查询是什么?我怀疑这是针对您自己的SQL注入攻击,您只需要参数化
SelectedValue
选项。首先,我猜测所选值有问题。您是否可以编辑您的问题并包含存在问题的实际SQL查询?查看查询通常可以清楚地看出问题所在。Hello@Matthew Haugen我尝试了另一种解决方案来避免sql注入,但同样它不起作用。我编辑了我的帖子,并添加了第二个代码,说明它也不起作用。请在执行之前检查您的最终查询是什么。然后在sql窗口中执行相同的查询。可能是类型转换错误。如果您知道您的查询是正确的,它可能会消失。所以,请告诉我们您的最终查询是什么,或者在sql窗口中执行它。您好@景观企鹅谢谢您的回复,但您能告诉我如何在Order BY please之前添加。我想您可以这样连接它:'come.CommandText=CarSqlST+condition+“Order BY[…]”。IMO,内联SQL确实不是一个理想的解决方案,但至少应该可以解决您的错误消息。
    string CarSqlST = @"SELECT [JobNum], [Dept], [PubDate], [EndDate], [Employer],[VacCountry], [VacState], [VacCity],
    [Jobtitle], [CompLogo], 
    SUBSTRING([jobdesc],1,40) 
    as jobdesc FROM [jobs] Where 1=1 and [VacCountry] = [@Location] ";

    ................


ccmd.CommandText = CarSqlST + condition+" ORDER BY [PubDate] DESC " ;
                SqlDataAdapter ad = new SqlDataAdapter();
                ad.SelectCommand = ccmd;

                ad.Fill(cdt);
                Joblistview.DataSource = cdt;
                Joblistview.DataBind();

            }