Daypilot - 从SQL Server加载约会

时间:2017-01-11 15:01:15

标签: c# sql-server calendar daypilot

我正在使用Daypilot创建一个主旋转。

我目前正在将约会存储在数据库中,但我希望检索某一天的所有约会并将其显示在日历上,与日期无关。

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天”的所有条目都应在星期一显示,与DATE无关

enter image description here

日历如下所示。如您所见,没有显示日期,因为如果DAY匹配

,它应该总是加载约会

enter image description here

有人可以协助我构建select语句,以便约会正确加载到日历上吗? E.G第1天约会到周一,第2天到周二等......

3 个答案:

答案 0 :(得分:1)

您必须在参数绑定中添加@

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

答案 1 :(得分: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始终是星期日)。

答案 2 :(得分: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();

        }
    }