2017-10-07 110 views
1

我想記錄的1天到30天的查詢是在SQL工作正常一天到30天C#

SELECT * FROM Tbl_order WHERE date >= DATEADD(day, -1, GETDATE()) 

所以我在C#沒有我使用組合框中選擇記錄

int CmboDays; 

for (CmboDays = -1; CmboDays >= -30; CmboDays--) 
      { 
       comboBox1.Items.Add(CmboDays); 
      } 

     private void orderBySearchDays() 
     { 
      string c = comboBox1.Text; 
      int x = Convert.ToInt32(c); 

      using (connection = new SqlConnection(connectionString)) 
      using (SqlDataAdapter adapter = new SqlDataAdapter("select* from Tbl_order WHERE date >= DATEADD(day,'"+ x + "', GETDATE()) ; ", connection)) 
      {     

       DataTable Tbl_order = new DataTable(); 
       connection.Open(); //opens the connection 
       adapter.Fill(Tbl_order); 
       connection.Close(); //Closes the connection 

       lst_CustomerNo.DataSource = Tbl_order; //assigns a datasource 
       lst_CustomerNo.DisplayMember = "CustomerNo"; //assigns display 
       lst_CustomerNo.ValueMember = "CustomerNo"; 

       lst_OrderName.DataSource = Tbl_order; 
       lst_OrderName.DisplayMember = "OrderName"; 
       lst_OrderName.ValueMember = "OrderName"; 

       lst_Quantity.DataSource = Tbl_order; 
       lst_Quantity.DisplayMember = "Quantity"; 
       lst_Quantity.ValueMember = "Quantity"; 

       lst_Price.DataSource = Tbl_order; 
       lst_Price.DisplayMember = "Price"; 
       lst_Price.ValueMember = "Price"; 

       lst_datetime.DataSource = Tbl_order; 
       lst_datetime.DisplayMember = "Date"; 
       lst_datetime.ValueMember = "Date"; 

      } 
     } 

因此所有的數據將在列表框中顯示

我得到這個錯誤

System.Data.dll中發生未處理的「System.Data.SqlClient.SqlException」類型異常

附加信息:參數數據類型varchar對於dateadd函數的參數2無效。

我在做那個錯嗎? 如何解決錯誤?

+1

使用的參數。不要使用輸入值查詢字符串。 –

回答

1

你能chnage與x.ToString X(),並刪除「

using (SqlDataAdapter adapter = new SqlDataAdapter("select * from Tbl_order WHERE date >= DATEADD(day,"+ x.ToString() + ", GETDATE()) ; ", connection)) 
+0

我試過了,但仍然一樣錯誤 –

+1

您是否在x之前和之後刪除了'char? –

+0

它工作^^謝謝。我的錯誤^^「 –