System.Data.SqlClient.SqlException:'关键字'''之间的语法不正确。

时间:2018-06-14 14:08:54

标签: c#

SqlDataAdapter sda = new SqlDataAdapter("Select * from salestable where empid='"
+ txtsid.Text
+ "'between'"
+ datepick1.Value.ToString("MM/dd/yyyy")
+ "'And'"
+ datepick2.Value.ToString("MM/dd/yyyy")
+ "'", con);


DataTable dt = new DataTable();
sda.Fill(dt);

3 个答案:

答案 0 :(得分:1)

尝试在sqlDataAdapter上使用参数:

        string SQLsentence = @"
SELECT * 
FROM salestable 
WHERE empid=@Employe
    AND [datesale(your column with date)] between @fDate And @lDate
";
        SqlDataAdapter sda = new SqlDataAdapter ( SQLsentence , con );

        sda.SelectCommand.Parameters.Add ( new SqlParameter ( "@Employe" , txtsid.Text ) );
        sda.SelectCommand.Parameters.Add ( new SqlParameter ( "@fDate" , new DateTime ( 2018 , 06 , 1 ) ) );
        sda.SelectCommand.Parameters.Add ( new SqlParameter ( "@lDate" , new DateTime ( 2018 , 06 , 14 ) ) );

使用参数阻止SQL注入。

答案 1 :(得分:0)

你似乎没有为两者之间的某个领域进行测试。看起来sql rendored将类似于:

Select * from salestable where empid='x'between'mm/dd/yy'and'mm/dd/yyyy'

你没有在这两个日期值之间测试任何东西。加上你的间距使得sql难以阅读

答案 2 :(得分:-1)

修正了它。我失踪了" AND"就在" BETWEEN"之前和表列名称" salesdate"

"从salestable中选择*,其中empid ='" + txtsid.Text +"' 和salesdate '& #34 + + DATE1"'和'" + date2 +"'"

谢谢大家试图帮助我。 Jegan