Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#从sql获取月度报告_C#_Sql Server - Fatal编程技术网

C#从sql获取月度报告

C#从sql获取月度报告,c#,sql-server,C#,Sql Server,我已经完成了年度报告,现在我需要总结访客的月度统计数据。我使用的datetimerpicker只有月份和年份 我不知道如何获得月度报告,所以我只包含一些看起来与数据库中相同的字符串 SqlCommand cmd = new SqlCommand("SELECT ISNULL(SUM(Male),0) as Male,ISNULL(SUM(Female),0) as Female ,ISNULL(SUM(Pax),0) as Pax,ISNULL(SUM(Single),0) as Sing

我已经完成了年度报告,现在我需要总结访客的月度统计数据。我使用的datetimerpicker只有月份和年份

我不知道如何获得月度报告,所以我只包含一些看起来与数据库中相同的字符串

SqlCommand cmd = new SqlCommand("SELECT ISNULL(SUM(Male),0) as Male,ISNULL(SUM(Female),0)  as Female ,ISNULL(SUM(Pax),0)  as Pax,ISNULL(SUM(Single),0)  as Single,ISNULL(SUM(Married),0)  as Married,ISNULL(SUM(Students),0)  as Students,ISNULL(SUM(Elementary),0)  as Elementary,ISNULL(SUM(Highschool),0)  as Highschool, ISNULL(SUM(College),0)  as College,ISNULL(SUM(PWD),0)  as PWD, ISNULL(SUM([AR Users]),0)  as ARUsers,ISNULL(SUM([12 Below]),0)  as age1,ISNULL(SUM([13-21]),0)  as age2,ISNULL(SUM([22-35]),0)  as age3,ISNULL(SUM([36-50]),0)  as age4,ISNULL(SUM([51-65]),0)  as age5,ISNULL(SUM([65 Above]),0)  as age6 FROM  [tbl_Registration] where [Date Added] >= '"
            + dtpMonth.Value.Month.ToString() + "/" + "01" + "/" + dtpMonth.Value.Year.ToString() + "' AND  [Date Added] <'" + dtpMonth.Value.AddMonths(1).Month.ToString() + "/" + "01" + "/" + dtpMonth.Value.Year.ToString() + "'  ;", connection);

在使用SQL查询时,可以对日期范围使用BETWEEN运算符,如

WHERE date BETWEEN firstDate AND lastDate
要得到一个月的第一个和最后一个日期,你可以简单地这样做

var firstDayOfMonth = new DateTime(year, month, 1);
var lastDayOfMonth = firstDayOfMonth.AddMonths(1).AddDays(-1);

您应该使用小于而不是大于。九月在十月之前,而不是之后。”2018年1月9日>=2018年1月10日代码正在运行,但也显示了其他年份的报告。ex.09/2018运行良好,但09/2017报告也显示了这一点。
var firstDayOfMonth = new DateTime(year, month, 1);
var lastDayOfMonth = firstDayOfMonth.AddMonths(1).AddDays(-1);