asp.net中的命令文本查询



我有一个月和年变量,我用这个查询获取数据。

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条件,我接受的数据已经被批准为真实的。我想知道是否有一种方法可以在不检查是否批准的情况下获取所有数据。我的意思是,我需要所有的数据和已批准为真实的数据。

如果您的意思是想要Approved和Not Approved的计数,那么您可以向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值来构造语句)。

相关内容

  • 没有找到相关文章

最新更新