简体   繁体   English

在SQL Server中使用联接和嵌套中继器?

[英]Using Joins and Nested Repeaters with SQL Server?

I am trying to create an Employee Scheduler, and am having some trouble displaying the data. 我正在尝试创建Employee Scheduler,并且在显示数据时遇到了一些麻烦。

I want to first display Schedule_Date s, and then underneath those, the Job_Type s that will be occurring on that day. 我想先显示Schedule_Date ,然后在其下显示当天要发生的Job_Type

Then, I am trying to display the Employee_Name , Start_Time & End_Time of that Schedule. 然后,我试图显示该计划的Employee_NameStart_TimeEnd_Time

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#: 这是我当前的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: 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: 以下是我的SQL表:

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: 首先,安装dapper(或另一个ORM),然后创建模型:

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: 创建您的DAL类:

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. 好的,现在您有了DAL,您可以将其按日期,然后按Job,雇员及其开始日期按所需的形式进行按摩。 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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