'<'附近的语法不正确在SQL查询中

时间:2014-02-16 07:38:14

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

       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_start_int

之间获取sql table列值time_end_int

我收到错误

  

'&lt;'附近的语法不正确。

2 个答案:

答案 0 :(得分:7)

您无法在SQL查询中编写start <= column <= end

你需要这样做:

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

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

答案 1 :(得分: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) 

我希望能帮助你。

相关问题