Daypilot - 从 SQL 服务器加载约会

Daypilot - Load appointments from SQL Server

我正在使用 Daypilot 创建主列表。

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

E.G 在星期一创建的所有约会都应始终在星期一显示,与 DATE 是什么无关。

当前select

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 "Day 1" 的所有条目都应显示在星期一,与 DATE

无关

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

,它应该始终加载约会

有人可以帮助我构建 select 语句,以便将约会正确加载到日历中吗?例如,第 1 天的约会到星期一,第 2 天到星期二等......

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

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

如果您显示的唯一数据是每周数据(并且唯一的视图是周视图),那么您可以使用以下方法:

加载约会时,使用 "Day" 字段计算自定义开始和结束日期并覆盖原始值。在 SQL 中很难做到这一点,但您可以将 GetAssignmentsForLocation() 修改为 post - 轻松处理数据集:

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 始终是星期日)。

另外,我修改了create,让它可以以星期六为开始日工作,这样它就不需要在星期天开始了。显然你可以改变它,这样任何一天都可以是 "starting day"

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

        }
    }