Complexe日期查询SQL

时间:2015-05-06 21:16:27

标签: mysql sql asp.net asp.net-mvc-3 razor

我想在此代码中添加日期过滤器:

var db = Database.Open("Tool") ;
var selectCommand = "SELECT Nom, COUNT(Nom) FROM DTool2 AS NumberOfOrders Group by Nom ";

var selectedData = db.Query(selectCommand);
var grid = new WebGrid(source: selectedData, defaultSort: "Column1", rowsPerPage:200); 
grid.SortDirection = SortDirection.Descending;

将从输入中选择日期(type = date)...

我知道要添加的代码如下所示:

Where Date between date1 and date2

我无法想象如何将其插入上面的代码

任何人都可以帮助我

谢谢

好的代码现在看起来像那样:

var db = Database.Open("Tool") ;
var selectCommand = "SELECT Nom, COUNT(Nom) FROM DTool2 AS NumberOfOrders GROUP BY Nom";   
var selectedData = db.Query(selectCommand);

if(!Request.QueryString["Date1"].IsEmpty() || !Request.QueryString["Date2"].IsEmpty()) {
  var date1 = Request["Date1"];
  var date2 = Request["Date2"];
  selectCommand = "SELECT Nom, COUNT(Nom) FROM DTool2 AS NumberOfOrders WHERE Date BETWEEN " + date1  + " AND " + date2  + " GROUP BY Nom";
  selectedData = db.Query(selectCommand);
}
  var grid = new WebGrid(source: selectedData, defaultSort: "Column1", rowsPerPage:200);
  grid.SortDirection = SortDirection.Descending;

尝试填充这两个输入时出错: Échecdela conversiondesdonnées。 [OLE DB状态值(如果已知)= 2]

1 个答案:

答案 0 :(得分:0)

Where子句应该添加到selectCommand查询中,如下所示:

SELECT Nom, COUNT(Nom) FROM DTool2 AS NumberOfOrders 
WHERE Date BETWEEN date1 and date2 GROUP BY Nom

使用输入表单字段的PFB查询:

var selectCommand = "SELECT Nom, COUNT(Nom) FROM DTool2 AS NumberOfOrders WHERE Date BETWEEN '" + date1  + "' AND '" + date2  + '" GROUP BY Nom";