c#从当前日期减去30天后出错?的WinForms

时间:2014-04-08 06:50:45

标签: c# sql winforms ms-access

我需要从当前日期减去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这个代码但是工作小错误就是&#34;&lt;&#34; &安培; &#34;&GT;&#34;这只是工作&#34; =&#34;标志不能在这段代码中工作?

非常感谢。

1 个答案:

答案 0 :(得分:0)

如果您使用访问权限作为数据库,那么请使用#not&#39;&#39; 还有一件事通过具有完整月份名称的日期字符串,这将忽略本地日期设置

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.ToString("dd/MMM/yyyy") + "# AND InvoiceDate <= #" +         
      curdate1.ToString("dd/MMM/yyyy") + "# ", connection);

DataSet sourceDataSet = new DataSet();
adapter.Fill(sourceDataSet);
gridControl1.DataSource = sourceDataSet.Tables[0];
相关问题