asp.net中的命令文本查询

时间:2015-12-17 07:40:00

标签: sql entitydatasource

我有一个月份和年份变量,我正在使用此查询获取数据。

EntityDataSourcePersonel.CommandText = 
"SELECT COUNT(TeklifTable.TeklifHazirlayan) AS Basari, EmployeeTable.Name, EmployeeTable.Surname, SUM(TeklifTable.TeklifTutar) AS ToplamSatis FROM EmployeeTable JOIN TeklifTable ON TeklifTable.TeklifHazirlayan = EmployeeTable.EmployeeId WHERE MONTH(TeklifTable.TeklifTarih) = "+dtM+" AND YEAR(TeklifTable.TeklifTarih) = " + dtY + " AND TeklifTable.Approved = true GROUP BY EmployeeTable.Name,EmployeeTable.Surname";

这个查询工作正常但我还需要更多东西。如你所见,我有一个where条件,我采用已批准为真的数据。我想知道是否有一种方法也可以获得所有数据,而无需检查它是否获得批准。我的意思是我需要所有已经批准为真实的数据和数据。

1 个答案:

答案 0 :(得分:1)

如果您的意思是需要“已批准”和“未批准”的计数,则可以在SELECT子句中添加列,如下所示;

EntityDataSourcePersonel.CommandText = 
"SELECT COUNT(TeklifTable.TeklifHazirlayan) AS Basari, 
        EmployeeTable.Name, 
        EmployeeTable.Surname, 
        SUM(TeklifTable.TeklifTutar) AS ToplamSatis,
        SUM(CASE WHEN TeklifTable.Approved = true THEN 1 ELSE 0 END) as [TotalApproved], 
        SUM(CASE WHEN TeklifTable.Approved = false THEN 1 ELSE 0 END) AS [TotalUnapproved] 
   FROM EmployeeTable 
   JOIN TeklifTable ON TeklifTable.TeklifHazirlayan = EmployeeTable.EmployeeId
  WHERE MONTH(TeklifTable.TeklifTarih) = "+dtM+" 
    AND YEAR(TeklifTable.TeklifTarih) = " + dtY + " 
GROUP BY EmployeeTable.Name,EmployeeTable.Surname";

请查看使用SqlParameter,因为您当前的命令可以通过SQL注入(通过附加dtMdtY值来构建语句)。