2012-05-20 49 views
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子句中傳遞兩個日期。如何通過使用ADO.NET使用複雜查詢

+0

您正在傳遞兩個日期。你真的在找什麼? – tzerb

+0

我會根據開始日期和結束日期得到一些統計數據。我想通過使用gridview將這些統計信息顯示給用戶。 – Ranjith

回答

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... 
+0

謝謝理查德。它爲我工作。 – Ranjith

+0

@Ranjith:如果它對你有用,那麼你應該做正確有禮貌的事情並[接受答案](http://meta.stackexchange.com/q/5234/153998) –