【问题标题】:Incorrect syntax near '<' in SQL querySQL 查询中“<”附近的语法不正确
【发布时间】:2014-03-15 12:55:40
【问题描述】:
       String start_cd;
       String end_cd;
       int time_start_int;
       int time_end_int;
        opencon();

        SqlCommand res = new SqlCommand("SELECT ID,Available,Type," + start_cd + "," + 
            end_cd + " FROM " + going + 
           " WHERE " + start_cd + "!=0 or " + end_cd + "!=0 and " + 
           time_start_int + " <= " + start_cd + " <= " + time_end_int + "", con);
        SqlDataAdapter sda_res = new SqlDataAdapter(res);
        DataTable dt_res = new DataTable();
        sda_res.Fill(dt_res);

        listBox1.DataSource=dt_res;
        listBox1.DisplayMember="ID";

        listBox2.DataSource = dt_res;
        listBox2.DisplayMember = start_cd;

我想在time_end_int之间获取sql表列值time_start_int

我遇到错误

【问题讨论】:

  • 不要连接你的字符串,这会让你容易受到 SQL 注入的攻击。
  • 转换 int ti 字符串,然后连接字符串。

标签: c# sql sql-server conditional-statements


【解决方案1】:

您想在两个值之间进行查询。 但是您键入的代码在 sql 中的语法不正确,而不是 C# 问题。

 SELECT [ID]
      ,[Available]
      ,[Type]     
  FROM [dbo].[Going]
  where start_cd !=0 or end_cd!=0 and time_end_int <= start_cd <= time_start_int 

测试这段代码:

    SELECT [ID]
      ,[Available]
      ,[Type]   FROM [dbo].[Going]
  where start_cd !=0 or end_cd!=0 and (time_end_int <= start_cd and  start_cd <= time_start_int) 

希望对你有帮助。

【讨论】:

    【解决方案2】:

    您不能在 SQL 查询中写入 start &lt;= column &lt;= end

    你需要做的:

    "...and start_cd >= " + time_start_int + " and " + start_cd + " <= " + time_end_int
    

    但请不要连接您的字符串,这会使您容易受到 SQL 注入的攻击。使用 SQL 参数。

    【讨论】:

    • 我不知道你的数据,这有点难说。
    猜你喜欢
    • 1970-01-01
    • 2017-02-26
    • 2013-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多