0
SELECT 
    Wot.WorkOrderType,
    COUNT(CASE WHEN Wo.Status =  'C' THEN 1 END) as [Completed],
    COUNT(CASE WHEN Wo.Status = 'E' THEN 1 END) as [Error]
FROM WorkOrder_2002 Wo
INNER Join  WorkOrderType Wot ON Wo.WorkOrderTypeId= wot.Id
WHERE Wo.CreateTime BETWEEN '2012-04-01' AND '2012-05-15'
GROUP BY Wo.WorkOrderTypeId,Wot.WorkOrderType
ORDER BY Wo.WorkOrderTypeId,Wot.WorkOrderType

我想使用上述查询通过 ADO.NET 检索记录(我想在 SqlCommand 中传递此查询)。我想在 where 子句中传递两个日期。

4

1 回答 1

2
SqlConnection connection = new SqlConnection(CONNECTIONSTRING);
connection.Open;

SqlCommand cmd = connection.CreateCommand();
cmd.CommandText = @"SELECT 
        Wot.WorkOrderType,
        COUNT(CASE WHEN Wo.Status =  'C' THEN 1 END) as [Completed],
        COUNT(CASE WHEN Wo.Status = 'E' THEN 1 END) as [Error]
    FROM WorkOrder_2002 Wo
    INNER Join  WorkOrderType Wot ON Wo.WorkOrderTypeId= wot.Id
    WHERE Wo.CreateTime BETWEEN @startdate AND @enddate
    GROUP BY Wo.WorkOrderTypeId,Wot.WorkOrderType
    ORDER BY Wo.WorkOrderTypeId,Wot.WorkOrderType";
cmd.Parameters.AddWithValue("@startdate", DateTime.Parse("2012-04-01"));
cmd.Parameters.AddWithValue("@enddate", DateTime.Parse("2012-05-15"));

// Do whatever you want with the SqlCommand now...
于 2012-05-20T14:46:39.217 回答