2014-02-16 67 views
0
 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; 

我想要得到的SQL表列值time_start_int我收到錯誤附近有語法錯誤 '<' 在SQL查詢

附近有語法錯誤<「time_end_int之間

+7

不要連接你的字符串,它會讓你容易受到SQL注入的攻擊。 – Szymon

+0

轉換int ti字符串,然後連接字符串。 –

回答

7

在SQL查詢中不能寫start <= column <= end

你需要做的:

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

但請不要串聯你的字符串,它讓你容易受到SQL注入。使用SQL參數。

+0

沒有給出錯誤,但沒有過濾器值 – sudhara

+0

由於我不知道你的數據,所以我有點難說。 – Szymon

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) 

我希望幫你。