簡體   English   中英

在SQL Server中使用聯接和嵌套中繼器?

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

我正在嘗試創建Employee Scheduler,並且在顯示數據時遇到了一些麻煩。

我想先顯示Schedule_Date ,然后在其下顯示當天要發生的Job_Type

然后,我試圖顯示該計划的Employee_NameStart_TimeEnd_Time

到目前為止,我已經顯示了日期和工作類型,但是在嘗試顯示員工姓名和開始/結束班次時遇到了麻煩。

以下是我當前的輸出: 片段1

這是我當前的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>

以下是我的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
);

還有,我的表數據:

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

首先,安裝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;}
}

創建您的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();
  }
}

好的,現在您有了DAL,您可以將其按日期,然后按Job,雇員及其開始日期按所需的形式進行按摩。 因此,讓我們繼續在后面的頁面代碼中執行此操作:

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;
}

現在讓我們制作中繼器並將其全部綁定:

<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