2014-12-07 125 views
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; 
    } 
+0

我相信你會得到一些比較幾個星期幾個字符串,關注點分離的評論,所以我會跳過所有的。如果您包含一些示例數據以及您的方法返回的內容(例如,如何證明它不能正常工作),可能會有幫助。 – Jedidja 2014-12-07 17:21:01

+0

可能您可以在數據庫級別執行此操作。看看這個問題:https://stackoverflow.com/questions/3769555/what-is-the-nicest-way-to-select-the-current-week-in-mysql – 2014-12-07 17:34:13

+0

檢查這個答案:http:// stackoverflow的.com /一個/897326分之15433121。更多關於datepart:http://msdn.microsoft.com/en-us/library/ms174420.aspx – Neolisk 2014-12-07 21:51:48

回答

1

不是分手的日期比較邏輯到月和日比較(其中有您所發現的bug)爲什麼不直接比較日期?

例如,這可以通過SQL參數來實現:

string sSql = "SELECT name, surname, date_birth FROM members WHERE date_birth BETWEEN @param_val_1 AND @param_val_2"; 
MySqlCommand command = Connect.getDataCommand(sSql, Conn); 
command.Parameters.AddWithValue("@param_val_1", weekStart); 
command.Parameters.AddWithValue("@param_val_2", weekEnd); 

您還可以簡化代碼,以獲得「當前」一週,以避免所有情況下的邏輯:

var today = DateTime.Today; 
// start with the numbers [0 .. 7) 
var startOfWeek = Enumerable.Range(0, count: 7) 
    // for each, subtract that many days from today 
    .Select(i => today.AddDays(-i)) 
    // find the first such date that is a Sunday 
    .First(dt => dt.DayOfWeek == DayOfWeek.Sunday); 
var endOfWeek = startOfWeek.AddDays(6); 

根據關於如何存儲數據,您可能還必須小心時間組件。 DateTime.Today爲您提供當天沒有時間組件(例如當天午夜)。但是,如果數據庫中的日期包含其他時間組件,則可能需要調整邏輯來解決這些問題。