我正在尝试创建一个表单,该表单将从数据库中获取考勤数据并传输到Excel工作表进行打印。
我已经创建了这个表单并且有两个datetimepickers我已经设法让这个月开始旅行索赔以及我想要声明的月份。
我正在努力将其分成单个月,以便我可以利用个别月份来调查数据库。
到目前为止我已经
了 if (TodateTimePicker.Value.Date <= FromdateTimePicker.Value.Date)
{
MessageBox.Show("End Date must be after Start Date");
}
else
{
fromDate = FromdateTimePicker.Value.Date;
toDate = TodateTimePicker.Value.Date;
fromDate = (fromDate.AddDays(1 - fromDate.Day));
toDate = (toDate.AddDays(1 - toDate.Day)).AddMonths(1).AddDays(-1)
}
我也有我想要使用的数据库命令
com = new OleDbCommand("SELECT count(*), FROM Attendances WHERE year([Attendance_Date]) = ? GROUP BY [Person] ORDER BY count(*) DESC", Program.DB_CONNECTION);
com.Parameters.Add(new OleDbParameter("", month));
com.Parameters.Add(new OleDbParameter("", p.ID));
我真正的问题是如何获得每个月的第1个月以及所选择的两个日期之间每个月的最后日期?
答案 0 :(得分:0)
以下是我找到的两种最简单的方法:
private int IntervalInMonthsBetweenDates(DateTime date1, DateTime date2)
{
return date2.Subtract(date1).Days / 30;
}
private int IntervalInMonthsBetweenDates(DateTime date1, DateTime date2)
{
return ((date1.Year - date2.Year) * 12) + date1.Month - date2.Month;
}
答案 1 :(得分:0)
如果您想要在DateTime
的年份和月份之间获得DateTimes
的序列,其中每个月的开头和月末都会显示,那么您可以执行以下操作。
public static IEnumerable<DateTime> BeginEndOfEachMonthBetween(DateTime from, DateTime to)
{
DateTime start = from.AddDays(1 - from.Day);
while(start <= to)
{
yield return start;
yield return start.AddMonths(1).AddDays(-1);
start.AddMonths(1);
}
}
但是看看你打算如何使用它会有所帮助。
听起来您可能想要一个与此类似的查询
SELECT
DatePart(year,Attendance_Date) As Year,
DatePart(month,Attendance_Date) As Month,
Count(*) As Attendance
FROM Attendances
WHERE Attendance_Date Between @BeginDate And @EndDate
GROUP BY
DatePart(year,Attendance_Date),
DatePart(month,Attendance_Date)
答案 2 :(得分:0)
我想出了一个解决方案,我计算了两个日期之间的月数,然后通过一个循环来增加开始日期的月数,以便给出两个月之间的每个月的第一天和最后一天原始日期,可能不是最好的方式,但它完全按照我的计划运作。
int months =((toDate.Year - fromDate.Year)* 12 + toDate.Month - fromDate.Month)+ 1; //在两个日期之间找到几个月的munber并添加一个
DateTime dateofclaim = new DateTime(); // define dateofclame to a new date
for (int i = 0; i < months; i++) // for each month run the code below, this will first run the fromDate then add a month until it gets to the toDate
{
dateofclaim = fromDate.AddMonths(i); // add one month to the start date
claimfromDate = (dateofclaim.AddDays(1 - dateofclaim.Day)); // calculate the first date of the month
claimtoDate = (dateofclaim.AddDays(1 - dateofclaim.Day)).AddMonths(1).AddDays(-1); // calculate the last day of the month
OleDbCommand com = new OleDbCommand("SELECT sum([sum]) FROM Attendance WHERE ([Attendance_Date]) >= ? AND ([Attendance_Date]) <= ? AND [Person] = ?", Program.DB_CONNECTION); // count the total attendances in the month
com.Parameters.Add(new OleDbParameter("", claimfromDate));
com.Parameters.Add(new OleDbParameter("", claimtoDate));
com.Parameters.Add(new OleDbParameter("", person.ID));
OleDbDataReader dr = com.ExecuteReader(); // start reader
if (dr.Read()) // if a result is returned
{
try
{
int attendance = Convert.ToInt32(dr.GetDouble(0)); // this is how many attendances have been counted for the selected month
ws.get_Range("B" + row.ToString()).Value2 = dateofclaim.ToString("MMM yyyy"); // print the Month of the clame into Colum B and row 83 then adding one for each following month
ws.get_Range("I" + row.ToString()).Value2 = attendance.ToString(); // print the number of attendances into Colum B and the respective row
row++; // add one to the row so next time this runs the data will be on the next row
}
catch // if nothing as returned from the database then add the month and 0 in the attendance colum
{
ws.get_Range("B" + row.ToString()).Value2 = dateofclaim.ToString("MMM yyyy");
ws.get_Range("I" + row.ToString()).Value2 = "0";
row++;
}
}
dr.Close(); // close the database reader
}
答案 3 :(得分:-1)
for (DateTime i = fromDate; i.Date <= toDate.Date; i = i.AddMonths(1))
{
Console.WriteLine(i.Month);
}