我希望获得1天到30天的记录,查询正在SQL正常工作
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.SqlClient.SqlException'未处理的异常发生在System.Data.dll
中附加信息:参数数据类型varchar对于dateadd函数的参数2无效。
我这样做错了吗? 如何修复错误?
答案 0 :(得分:1)
你可以用x.ToString()来删除x并删除'
using (SqlDataAdapter adapter = new SqlDataAdapter("select * from Tbl_order WHERE date >= DATEADD(day,"+ x.ToString() + ", GETDATE()) ; ", connection))