【发布时间】:2014-04-08 06:50:45
【问题描述】:
我需要从当前日期减去 30 天,然后我过滤查询,但我没有得到任何结果/输出为什么我在此代码上的错误
DateTime curdate = DateTime.Now;
curdate = curdate.AddDays(-30); // if i give -4 instead of -30 the query will bind data
DateTime curdate1 = DateTime.Now;
validateDept.InitializeConnection();
OleDbConnection connection = new OleDbConnection(validateDept.connetionString);
OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT InvoiceId, InvoiceNumber, InvoiceDate, (Select CustomerId from Customer Where Customer.CustomerId=NewInvoice_1.CustomerName) AS CustomerId, (Select CustomerName from Customer where Customer.CustomerId = NewInvoice_1.CustomerName) AS CustomerName, DueDate, Tax, GrandTotal, CompanyId FROM NewInvoice_1 WHERE InvoiceDate >= '" + curdate + "' AND InvoiceDate <= '" + curdate1 + "' ", connection);
DataSet sourceDataSet = new DataSet();
adapter.Fill(sourceDataSet);
gridControl1.DataSource = sourceDataSet.Tables[0];
空表仅在我运行此代码时显示。如果我将 -30 更改为 -4,那么它会从 Access DB 中获取一行。从 4 月 1 日到当前日期 4 月 8 日,如果我们给出 -3、-4、-5、-6、-7 这段代码但可以工作的小错误是 "" 这只能工作 "=" 符号不能工作这段代码?
非常感谢。
【问题讨论】:
-
使用参数错误是因为您将其转换为字符串,这取决于日期的格式......以及服务器中的格式,还阅读了有关 sql 注入的信息,所以您是没有通过日期,你正在通过一个 stng 这就是为什么不工作
-
除了@Mr. - 有关示例,请参阅 OleDbParameter。
-
@Mr. - 鉴于参数是日期,很难进行 SQL 注入 - 但仍然同意参数是正确的做法
-
@greg 我知道,为了他自己的利益,搜索他为什么不应该以这种方式使用内联参数或查询,因为树枝是弯曲的,所以树会生长
-
@Sri - 在OleDbParameter 的链接页面上,您将找到有关如何准确使用它们的示例。只需告诉参数,它应该是什么类型(在你的情况下是“DateTime”或类似的)并将值放在参数的
Value属性中。