2017-01-11 49 views
0

我正在使用Daypilot創建主輪盤。Daypilot - 從SQL Server加載約會

我目前在數據庫中存儲約會,但我想檢索某一天的所有約會,並將它們顯示在與日期無關的日曆上。

E.G星期一創建的所有約會應始終顯示在星期一,與DATE無關。

當前選擇

public DataTable GetAssignmentsForLocation(DayPilotCalendar calendar) 
{ 
    DataTable dt = new DataTable(); 

    var da = CreateDataAdapter("select * from [master_rota] where [LocationId] = @location and Week = @Week"); 

    AddParameterWithValue(da.SelectCommand, "location", (int)calendar.ClientState["location"]); 
    AddParameterWithValue(da.SelectCommand, "week", (int)calendar.ClientState["week"]); 

    da.Fill(dt); 

    return dt; 
} 

而且

protected void DayPilotCalendar1_Command(object sender, CommandEventArgs e) 
{ 
    switch (e.Command) 
    { 
     case "navigate": 
      var start = (DateTime)e.Data["start"]; 
      DayPilotCalendar1.StartDate = start; 
      DayPilotCalendar1.DataSource = new DataManager_MasterRota().GetAssignmentsForLocation(DayPilotCalendar1); 
      DayPilotCalendar1.DataBind(); 
      DayPilotCalendar1.Update(); 
      break; 
     case "refresh": 
      DayPilotCalendar1.DataSource = new DataManager_MasterRota().GetAssignmentsForLocation(DayPilotCalendar1); 
      DayPilotCalendar1.DataBind(); 
      DayPilotCalendar1.Update(); 
      break; 
     case "day": 
      DayPilotCalendar1.ViewType = ViewTypeEnum.Day; 
      DayPilotCalendar1.StartDate = (DateTime)e.Data["date"]; 

      DayPilotCalendar1.DataSource = new DataManager_MasterRota().GetAssignmentsForLocation(DayPilotCalendar1); 
      DayPilotCalendar1.DataBind(); 
      DayPilotCalendar1.Update(); 
      break; 
     case "week": 
      DayPilotCalendar1.ViewType = ViewTypeEnum.Week; 
      DayPilotCalendar1.DataSource = new DataManager_MasterRota().GetAssignmentsForLocation(DayPilotCalendar1); 
      DayPilotCalendar1.DataBind(); 
      DayPilotCalendar1.Update(); 
      break; 

    } 
} 

創作

public void CreateAssignment(DateTime start, DateTime end, int location, int week, int person, string note, DayOfWeek day) 
{ 
    using (DbConnection con = CreateConnection()) 
    { 
     con.Open(); 

     // string id = ""; 
     var cmd = CreateCommand("insert into [master_rota] ([AssignmentStart], [AssignmentEnd], [LocationId], [PersonId], [AssignmentNote], week, day) values (@start, @end, @location, @person, @note, @Week, @day)", con); 
     AddParameterWithValue(cmd, "start", start); 
     AddParameterWithValue(cmd, "end", end); 
     AddParameterWithValue(cmd, "location", location); 
     AddParameterWithValue(cmd, "week", week); 
     AddParameterWithValue(cmd, "person", person); 
     AddParameterWithValue(cmd, "note", note); 
     AddParameterWithValue(cmd, "day", day); 

     cmd.ExecuteScalar(); 
    } 
} 

數據庫條目:數據庫記錄每個條目的日子,所以理論上它應該是可能的加載給定日期值的所有值

E.G爲「1天」的所有條目,都應該顯示在週一,不相關的日期的

enter image description here

日曆看起來像下面。正如你可以看到有沒有顯示日期,如果有一天匹配

enter image description here

可能有人請幫助我建立select語句,以便任命加載到日曆正確應該總是加載預約嗎? EG第1天預約到週一,2日週二等等....

+1

你沒有向我們展示填充日曆的代碼嗎?你只是在填寫一張DataTable –

+0

現在就添加它 –

+0

這只是INSERT,你用什麼代碼將數據表分配給日曆? –

回答

1

如果要顯示的唯一數據是該週數據(和唯一的視圖是周視圖),那麼你可以使用以下方法:

加載約會時,使用「日」字段來計算自定義開始和結束日期並覆蓋原始值。這將是很難做到的,在SQL,但你可以修改GetAssignmentsForLocation()進行後處理數據輕鬆設置:

public DataTable GetAssignmentsForLocation(DayPilotCalendar calendar) 
{ 
    DataTable dt = new DataTable(); 

    var da = CreateDataAdapter("select * from [master_rota] where [LocationId] = @location and Week = @Week"); 

    AddParameterWithValue(da.SelectCommand, "@location", (int)calendar.ClientState["location"]); 
    AddParameterWithValue(da.SelectCommand, "@week", (int)calendar.ClientState["week"]); 

    da.Fill(dt); 

    foreach (DataRow dr in dt.Rows) { 
     int dayOfWeek = (int) dr["Day"]; 
     DateTime start = (DateTime) dr["AssignmentStart"]; 
     DateTime end = (DateTime) dr["AssignmentEnd"]; 

     TimeSpan duration = end - start; 
     TimeSpan startTime = start.TimeOfDay; 

     dr["AssignmentStart"] = calendar.VisibleStart.AddDays(dayOfWeek).AddTime(startTime); 
     dr["AssignmentEnd"] = calendar.VisibleStart.AddDays(dayOfWeek).AddTime(startTime).AddTime(duration); 

    } 

    return dt; 
} 

請注意,這僅適用於週日爲一週的第一天(即calendar.VisibleStart總是星期天)。

+0

驚人的,這個作品。非常感謝,你不知道這會對我們組織有多大幫助。再次感謝。 –

1

您必須添加@您的參數域:

AddParameterWithValue(da.SelectCommand, "@location", (int)calendar.ClientState["location"]); 
AddParameterWithValue(da.SelectCommand, "@week", (int)calendar.ClientState["week"]); 
+0

好的皮卡,我還會添加使用一個spoc將更有效率與指數的位置和星期 –

+0

嗨,謝謝,這有助於該部分,但它並不能解決我原來的問題,我要求 –

1

此外,我修改了創建,以便它可以通過將星期六作爲開始日期工作,因此它不需要在星期天開始。顯然你可以改變它,以便任何一天都可以成爲「開始日」

public void CreateAssignment(DateTime start, DateTime end, int location, int week, int person, string note, DayOfWeek day) 
    { 
     using (DbConnection con = CreateConnection()) 
     { 
      con.Open(); 
      //string id = ""; 

      var cmd = CreateCommand("insert into [master_rota] ([AssignmentStart], [AssignmentEnd], [LocationId], [PersonId], [AssignmentNote], week, day) values (@start, @end, @location, @person, @note, @Week, @day)", con); 
      AddParameterWithValue(cmd, "start", start); 
      AddParameterWithValue(cmd, "end", end); 
      AddParameterWithValue(cmd, "location", location); 
      AddParameterWithValue(cmd, "week", week); 
      AddParameterWithValue(cmd, "person", person); 
      AddParameterWithValue(cmd, "note", note); 
      if (day == DayOfWeek.Saturday) 
      { 
       AddParameterWithValue(cmd, "day", day - 6); 
      } 
      else 
      AddParameterWithValue(cmd, "day", day + 1); 

      cmd.ExecuteScalar(); 

     } 
    }