0
我有一個程序,顯示每週的成員的每週生日。此代碼正常運行,直到本月底。每當我們到達月底並且當前月份還剩下不到7天時,代碼不再列出生日。每月從數據庫獲取每週生日的問題
我知道這是因爲本週現在跨越兩個月,但我已經嘗試了一切,只是無法弄清楚我必須做什麼才能解決這個問題。這可能是一個簡單的問題。任何幫助將不勝感激。這裏是我的代碼:
public void CheckDayOfWeekAndReturnBirthdaysInDateRange()
{
//Check the current day of the week and make calculations according to find out which week to use.
if (DateTime.Now.DayOfWeek.ToString() == "Sunday")
{
DateTime weekStart = DateTime.Now.Date;
DateTime weekEnd = DateTime.Now.AddDays(7);
Birthdays = eventsService.ReturnBirthdaysForCurrentWeek(weekStart, weekEnd);
}
else if (DateTime.Now.DayOfWeek.ToString() == "Monday")
{
DateTime weekStart = DateTime.Now.AddDays(-1);
DateTime weekEnd = DateTime.Now.AddDays(6);
Birthdays = eventsService.ReturnBirthdaysForCurrentWeek(weekStart, weekEnd);
}
else if (DateTime.Now.DayOfWeek.ToString() == "Tuesday")
{
DateTime weekStart = DateTime.Now.AddDays(-2);
DateTime weekEnd = DateTime.Now.AddDays(5);
Birthdays = eventsService.ReturnBirthdaysForCurrentWeek(weekStart, weekEnd);
}
else if (DateTime.Now.DayOfWeek.ToString() == "Wednesday")
{
DateTime weekStart = DateTime.Now.AddDays(-3);
DateTime weekEnd = DateTime.Now.AddDays(4);
Birthdays = eventsService.ReturnBirthdaysForCurrentWeek(weekStart, weekEnd);
}
else if (DateTime.Now.DayOfWeek.ToString() == "Thursday")
{
DateTime weekStart = DateTime.Now.AddDays(-4);
DateTime weekEnd = DateTime.Now.AddDays(3);
Birthdays = eventsService.ReturnBirthdaysForCurrentWeek(weekStart, weekEnd);
}
else if (DateTime.Now.DayOfWeek.ToString() == "Friday")
{
DateTime weekStart = DateTime.Now.AddDays(-5);
DateTime weekEnd = DateTime.Now.AddDays(2);
Birthdays = eventsService.ReturnBirthdaysForCurrentWeek(weekStart, weekEnd);
}
else if (DateTime.Now.DayOfWeek.ToString() == "Saturday")
{
DateTime weekStart = DateTime.Now.AddDays(-6);
DateTime weekEnd = DateTime.Now.AddDays(1);
Birthdays = eventsService.ReturnBirthdaysForCurrentWeek(weekStart, weekEnd);
}
}
public void LoadDatagridComponents(DataGridView dataBirthdays)
{
dataBirthdays.AutoGenerateColumns = false;
dataBirthdays.Columns[0].Visible = true;
dataBirthdays.Columns[1].Visible = true;
dataBirthdays.Columns[2].Visible = true;
dataBirthdays.Columns[0].HeaderText = "Name";
dataBirthdays.Columns[1].HeaderText = "Surname";
dataBirthdays.Columns[2].HeaderText = "Birthday";
//Loop through each birthday member and change date of birth to month/day fromat.
int currentRow = 0;
foreach (DataGridViewRow row in dataBirthdays.Rows)
{
string birthday = Convert.ToDateTime(dataBirthdays.Rows[currentRow].Cells[2].Value).ToString("MMMM dd");
dataBirthdays.Rows[currentRow].Cells[2].Value = birthday;
currentRow++;
}
}
然後從那裏到這裏:
public List<Birthdays> ReturnBirthdaysForCurrentWeek(DateTime weekStart, DateTime weekEnd)
{
var birthdays = new List<Birthdays>();
int month = DateTime.Now.Month;
int dayStartOfWeek = weekStart.Day;
int dayEndOfWeek = weekEnd.Day;
using (MySqlConnection Conn = new MySqlConnection(Connect.sConnStr))
{
Conn.Open();
string sSql = "SELECT name, surname, date_birth FROM members WHERE MONTH(date_birth) = " + month + " AND DAY(date_birth) BETWEEN " + dayStartOfWeek + " AND " + dayEndOfWeek + ";";
MySqlDataReader reader = Connect.getDataCommand(sSql, Conn).ExecuteReader();
while (reader.Read())
{
var birthday = new Birthdays()
{
MemberName = reader["name"].ToString(),
MemberSurname = reader["surname"].ToString(),
MemberBirthday = reader["date_birth"].ToString()
};
birthdays.Add(birthday);
}
Conn.Close();
Conn.Dispose();
}
return birthdays;
}
我相信你會得到一些比較幾個星期幾個字符串,關注點分離的評論,所以我會跳過所有的。如果您包含一些示例數據以及您的方法返回的內容(例如,如何證明它不能正常工作),可能會有幫助。 – Jedidja 2014-12-07 17:21:01
可能您可以在數據庫級別執行此操作。看看這個問題:https://stackoverflow.com/questions/3769555/what-is-the-nicest-way-to-select-the-current-week-in-mysql – 2014-12-07 17:34:13
檢查這個答案:http:// stackoverflow的.com /一個/897326分之15433121。更多關於datepart:http://msdn.microsoft.com/en-us/library/ms174420.aspx – Neolisk 2014-12-07 21:51:48