简体   繁体   中英

Using Joins and Nested Repeaters with SQL Server?

I am trying to create an Employee Scheduler, and am having some trouble displaying the data.

I want to first display Schedule_Date s, and then underneath those, the Job_Type s that will be occurring on that day.

Then, I am trying to display the Employee_Name , Start_Time & End_Time of that Schedule.

So far, I have the Dates & Job Types displaying, but I am having trouble trying to display the employee names & start/end shift times.

Below is my current output: 片段1

Here is my current C#:

 private void bindRepeaterPerPerson()
{
    conn = new SqlConnection(connectionString);
    conn.Open();
    comm = new SqlCommand("SELECT DISTINCT Start_Date, End_Date FROM My_Subscription WHERE Subscription_Id = 1", conn);
    SqlDataReader reader = comm.ExecuteReader();
    while (reader.Read())
    {
        startDate = Convert.ToDateTime(reader["Start_Date"]);
        endDate = Convert.ToDateTime(reader["End_Date"]);
    }
    conn.Close();
    using (SqlConnection con = new SqlConnection(connectionString))
    { 
        using (SqlCommand cmd = new SqlCommand("SELECT DISTINCT CAST(Schedule_Date AS DATE) As Schedule_Date FROM My_Employee_Schedule WHERE Start_Time BETWEEN @startReportDate AND @endReportDate", con))
        {
            cmd.Parameters.AddWithValue("@startReportDate", startDate);
            cmd.Parameters.AddWithValue("@endReportDate", endDate);
            using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
            {
                DataTable dt = new DataTable();
                sda.Fill(dt);
                repSubscription1.DataSource = dt;
                repSubscription1.DataBind();
            }
        }
    }
}

protected void repSubscription1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    conn = new SqlConnection(connectionString);
    conn.Open();

    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        Repeater repJobs = (Repeater)(e.Item.FindControl("repJobs"));
        SqlCommand cmd = new SqlCommand("SELECT DISTINCT j.Job_Title FROM [dbo].[My_Employee_Schedule] as s INNER JOIN My_Job_Type j on s.Job_ID = j.Job_Type_Id WHERE Schedule_Date = @Date", conn);
        DateTime myDate = Convert.ToDateTime(DataBinder.Eval(e.Item.DataItem, "Schedule_Date"));
        cmd.Parameters.AddWithValue("@Date", myDate);
        SqlDataAdapter sda = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        sda.Fill(dt);
        repJobs.DataSource = dt;
        repJobs.DataBind();
    }
    conn.Close(); 
}

HTML:

 <asp:Repeater ID="repSubscription1" runat="server" OnItemDataBound="repSubscription1_ItemDataBound">
                    <ItemTemplate>
                        <div class="col-lg-2">
                            <div class="panel panel-default">
                                <div class="panel-heading" style="background-color: #3A6EA5; color: white">
                                    <h4 class="panel-title">
                                        <%# Eval("Schedule_Date", "{0:dddd, dd MMMM yyyy}") %>
                                    </h4>
                                </div>
                                <!--panel-heading-->
                                <div class="panel-body">
                                    <asp:Repeater ID="repJobs" runat="server">
                                        <ItemTemplate>
                                            <%# Eval("Job_Title") %>
                                            <br /><br />
                                        </ItemTemplate>
                                    </asp:Repeater>

                                </div>
                                <!--panel-body-->
                            </div>
                            <!--panel-default-->
                        </div>
                        <!--col-lg-2-->
                    </ItemTemplate>
                </asp:Repeater>

Below are my SQL tables:

CREATE TABLE [dbo].[My_Employee_Schedule] (
[Emp_Sch_Id]    INT      IDENTITY (1, 1) NOT NULL,
[Schedule_Date] DATETIME NULL,
[Start_Time]    DATETIME NULL,
[End_Time]      DATETIME NULL,
[Emp_ID]        INT      NULL,
[Job_ID]        INT      NULL,
PRIMARY KEY CLUSTERED ([Emp_Sch_Id] ASC),
FOREIGN KEY ([Emp_ID]) REFERENCES [dbo].[My_Employee] ([Employee_Id]) ON 
DELETE CASCADE,
FOREIGN KEY ([Job_ID]) REFERENCES [dbo].[My_Job_Type] ([Job_Type_Id]) ON 
DELETE CASCADE
);

CREATE TABLE [dbo].[My_Job_Type] (
[Job_Type_Id] INT           IDENTITY (1, 1) NOT NULL,
[Job_Title]   NVARCHAR (50) NOT NULL,
 PRIMARY KEY CLUSTERED ([Job_Type_Id] ASC)
);

CREATE TABLE [dbo].[My_Employee] (
[Employee_Id]   INT           IDENTITY (1, 1) NOT NULL,
[Employee_Name] NVARCHAR (50) NOT NULL,
[Company_Id]    INT           NOT NULL,
PRIMARY KEY CLUSTERED ([Employee_Id] ASC),
FOREIGN KEY ([Company_Id]) REFERENCES [dbo].[My_Company] ([Company_Id]) ON DELETE CASCADE
);

And also, my table data:

Emp_Sch_Id  | Schedule_Date       | Start_Time          | End_Time            | Emp_ID | Job_ID
------------------------------------------------------------------------------------------------
1           | 03/06/2017 00:00:00 | 03/06/2017 09:00:00 | 03/06/2017 10:00:00 | 5      |  2
2           | 03/06/2017 00:00:00 | 03/06/2017 11:30:00 | 03/06/2017 12:30:00 | 6      |  1
3           | 03/06/2017 00:00:00 | 03/06/2017 14:00:00 | 03/06/2017 15:00:00 | 5      |  3
4           | 03/06/2017 00:00:00 | 03/06/2017 12:00:00 | 03/06/2017 13:00:00 | 4      |  2
5           | 03/06/2017 00:00:00 | 03/06/2017 12:15:00 | 03/06/2017 15:15:00 | 4      |  2

Employee_Id | Employee_Name
----------------------------
4           | Damien
5           | John
6           | Owen

First, install dapper (or another ORM), and create your model:

public class MyData {
  public DateTime Schedule_Date {get;set;}
  public DateTime Start_Time {get;set;}
  public DateTime End_Time {get;set;}
  public string Employee_Name {get;set;}
  public string Job_Title {get;set;}
}

Create your DAL class:

public class DAL
{
  public static string connectionString = ...;
  public IEnumberable<MyData> GetMyClassByRange(DateTime start, DateTime end)
  {
    var sql = @"
SELECT Schedule_Date, Start_Time, End_Time, Employee_Name, Job_Title
FROM My_Employee_Schedule ed
JOIN My_Employee e
  ON ed.Emp_ID=e.Employee_Id
JOIN My_Job_Type jt
  ON ed.Job_ID=jt.Job_Type_Id
WHERE Start_Time BETWEEN @startReportDate AND @endReportDate";
    conn = new SqlConnection(connectionString);
    conn.Open();
    var data = conn.Query<MyData>(sql, new { startReportDate=startDate, endReportDate=endDate});
    conn.Close();
  }
}

Ok, now that you have your DAL, you can massage it into the form that you want, which is by date, then by Job, with employees and their start dates. So let's go ahead and do that in your page code behind:

public IEnumerable<MyViewModel> GetData()
{
    //var test = new List<MyData>();
    //test.Add(new MyData { Schedule_Date = new DateTime(2017, 6, 3), Start_Time = new DateTime(2017, 6, 3, 9, 0, 0), End_Time = new DateTime(2017, 6, 3, 10, 0, 0), Job_Title = "Waiter", Employee_Name = "John" });
    //test.Add(new MyData { Schedule_Date = new DateTime(2017, 6, 3), Start_Time = new DateTime(2017, 6, 3, 11, 0, 0), End_Time = new DateTime(2017, 6, 3, 12, 0, 0), Job_Title = "Driver", Employee_Name = "Own" });
    //test.Add(new MyData { Schedule_Date = new DateTime(2017, 6, 3), Start_Time = new DateTime(2017, 6, 3, 14, 0, 0), End_Time = new DateTime(2017, 6, 3, 15, 0, 0), Job_Title = "Busser", Employee_Name = "John" });
    //test.Add(new MyData { Schedule_Date = new DateTime(2017, 6, 3), Start_Time = new DateTime(2017, 6, 3, 12, 0, 0), End_Time = new DateTime(2017, 6, 3, 13, 0, 0), Job_Title = "Waiter", Employee_Name = "Damien" });
    //test.Add(new MyData { Schedule_Date = new DateTime(2017, 6, 3), Start_Time = new DateTime(2017, 6, 3, 12, 15, 0), End_Time = new DateTime(2017, 6, 3, 15, 15, 0), Job_Title = "Waiter", Employee_Name = "Damien" });
    var MyDal = new DAL();
    var test = MyDal.GetMyClassRange(DateTime.Now, DateTime.Now.AddYears(1));
    var result = test
        .GroupBy(x => new {x.Schedule_Date, x.Job_Title})
        .GroupBy(x => x.Key.Schedule_Date)
        .Select(x => new MyViewModel {Start_Date = x.Key, Jobs = x.Select(y => new MyJobsView {Job_Title = y.Key.Job_Title, Datas = y})})
        .ToList();

    return result;
}

And now let's make your repeaters and bind it all up:

<asp:Repeater ID="rptDate" runat="server" ItemType="WebApplication1.MyViewModel" SelectMethod="GetData">
    <ItemTemplate>
        <br/>
        Start Date: <%# Item.Start_Date %>
        <asp:Repeater runat="server" ID="rptJob" ItemType="WebApplication1.MyJobsView" DataSource="<%# Item.Jobs %>">
            <ItemTemplate>
                <br/>Job Title: <%# Item.Job_Title %>
                <asp:Repeater runat="server" ID="rptJob" ItemType="WebApplication1.MyData" DataSource="<%# Item.Datas %>">
                    <ItemTemplate>
                        Name: <%# Item.Employee_Name %>
                        Start: <%# Item.Start_Time %>
                    </ItemTemplate>
                </asp:Repeater>
            </ItemTemplate>
        </asp:Repeater>
    </ItemTemplate>
</asp:Repeater>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM