简体   繁体   中英

C# Get monthly reports from sql

I'm already done with the yearly reports and now I need to sum up the monthly statistics of visitors. I am using a datetimerpicker with month and year only.

I have no idea how to get the monthly reports so I just concatinate some string that will look the same as in the database.

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);

as a result, its just like '9/01/2018 >= 10/01/2018 and I know it's a dumb thing to do.

每月报告

UPDATE THIS CODE IS WORKING BUT IT ALSO SHOWS THE MONTHLY REPORTS OF OTHER YEAR ex: 09/2018 is working but when dtpicker is 09/2017 it also shows the reports of 09/2018. Is there something wrong with my query? Any solutions?

var FirstDayOfMonth = new DateTime(dtpMonth.Value.Year, dtpMonth.Value.Month, 1);
 var LastDayOfMonth = FirstDayOfMonth.AddMonths(1).AddDays(-1);

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] BETWEEN '"
            + FirstDayOfMonth.ToShortDateString() + "' AND  '" + LastDayOfMonth.ToShortDateString() + "'  ;", connection);

As you are using SQL queries, you can use the BETWEEN operator for date range, like

WHERE date BETWEEN firstDate AND lastDate

and to get the first and last dates of a month, you can simply do this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM