0

我正在使用 Daypilot 创建一个主轮班表。

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

EG 在星期一创建的所有约会都应始终显示在星期一,与 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();
    }
}

数据库条目:数据库记录每个条目的日期,因此理论上应该可以加载给定日期值的所有值

EG “第 1 天”的所有条目,都应在星期一显示,与 DATE 无关

在此处输入图像描述

日历如下所示。如您所见,没有显示日期,因为如果 DAY 匹配,它应该始终加载约会

在此处输入图像描述

有人可以帮助我建立选择语句,以便约会正确加载到日历上吗?EG 第 1 天到周一,第 2 天到周二等......

4

3 回答 3

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();

        }
    }
于 2017-01-12T11:54:04.977 回答
1

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

 AddParameterWithValue(da.SelectCommand, "@location", (int)calendar.ClientState["location"]);
 AddParameterWithValue(da.SelectCommand, "@week", (int)calendar.ClientState["week"]);
于 2017-01-11T15:06:59.950 回答
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 始终为星期日)。

于 2017-01-11T16:26:45.907 回答