从连接的SQL Server表中无法正确检索数据

时间:2017-02-12 16:50:53

标签: c# sql sql-server

我正在尝试使用下表来检索以下内容:

  • “群组”表格中的所有Group_Names
  • Photo_Collection表中链接到组的所有名称(通过Group_Id)
  • Group_Images表中链接到Photo_Collection的所有文件名(通过Group_Image_Id)。

Here是当前正在显示的内容

here是样本表数据。

图像位于正确的群组(红十字会和枪支俱乐部)之下。

但是,每张图片上方的Photo_Collection名称都不正确。这些名称与这些图像无关。

此外,这是表格结构:

CREATE TABLE [dbo].[Groups] 
(
    [Group_Id] INT IDENTITY (1, 1) NOT NULL,
    [Group_Name] NVARCHAR (50)  NULL,
    [Group_Desc] NVARCHAR (MAX) NULL,

    CONSTRAINT [Groups.Group_Id.PrimaryKey] 
        PRIMARY KEY CLUSTERED ([Group_Id] ASC)
);

CREATE TABLE [dbo].[Group_Images] 
(
    [ID] INT IDENTITY (1, 1) NOT NULL,
    [Group_Id] INT NOT NULL,
    [filename] VARCHAR (250) NULL,
    [imageDesc] NVARCHAR (250) NULL,

    CONSTRAINT [Group_Images.ID.Primary Key] 
        PRIMARY KEY CLUSTERED ([ID] ASC),
    CONSTRAINT [Group_Images.to.Groups] 
        FOREIGN KEY ([Group_Id]) REFERENCES [dbo].[Groups] ([Group_Id])
);

CREATE TABLE [dbo].[Photo_Collection] 
(
    [Id] INT IDENTITY (1, 1) NOT NULL,
    [Group_Id] INT NOT NULL,
    [Name] NVARCHAR(250) NULL,

    CONSTRAINT [Photo_Collection.Id.PrimaryKey] 
        PRIMARY KEY CLUSTERED ([Id] ASC),
    CONSTRAINT [Photo_Collection.to.Groups] 
        FOREIGN KEY ([Group_Id]) REFERENCES [dbo].[Groups] ([Group_Id])
);

CREATE TABLE [dbo].[Photo_Collection_Images] 
(
    [Photo_Collection_Id] INT NOT NULL,
    [Group_Image_Id]      INT NOT NULL,

    CONSTRAINT [Photo_Collection_Images.to.Photo_Collection] 
        FOREIGN KEY ([Photo_Collection_Id]) REFERENCES [dbo].[Photo_Collection] ([Id]),
    CONSTRAINT [Photo_Collection_Images.to.Group_Images]  
        FOREIGN KEY ([Group_Image_Id]) REFERENCES [dbo].[Group_Images] ([ID])
);

这是我的ASPX:

<asp:Repeater ID="repGroupPhotoGallery" runat="server" OnItemDataBound="repGroupPhotoGallery_ItemDataBound">
    <ItemTemplate>
        <div class="col-md-3">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h3 class="panel-title">
                    <%# Eval("Group_Name") %>
                    </h3>
                </div>
                <!--panel-heading-->
                <div class="panel-body">
                    <div class="col-md-4 text-center">
                        <div class="thumbnail">
                            <asp:DataList ID="dlImages" runat="server" RepeatDirection="Horizontal" RepeatColumns="3" CellPadding="5">
                                <ItemTemplate>
                                    <div class="caption">
                                        <h3><%# Eval("pc_name") %><br />
                                        </h3>
                                        <a id="imageLink" href='<%# Eval("si_filename","/Group_Images/{0}") %>' title='<%#Eval("si_description") %>' rel="lightbox[Brussels]">
                                            <asp:Image ID="Image1" ImageUrl='<%# Bind("si_filename", "~/Group_Images/{0}") %>' runat="server" Width="112" Height="84" />
                                        </a>
                                        <p><%# Eval("si_description") %></p>
                                        <p><%# Eval("s_Id") %></p>
                                    </div>
                                </ItemTemplate>
                            </asp:DataList>
                        </div>
                    </div>
                </div>
            </div>
            <!--panel-default-->
        </div>
        <!--col-md-3-->
    </ItemTemplate>
</asp:Repeater>

这是我背后的代码:

private void BindRepeater()
{
    string constr = ConfigurationManager.ConnectionStrings["FYPConnectionString1"].ConnectionString;

    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlCommand cmd = new SqlCommand())
        {
            //ORDER BY DATE
            cmd.CommandText = "SELECT * FROM Groups WHERE Group_Type ='Group'";
            cmd.Connection = con;

            con.Open();

            repGroupPhotoGallery.DataSource = cmd.ExecuteReader();
            repGroupPhotoGallery.DataBind();

            con.Close();
        }
    }
}
protected void repGroupPhotoGallery_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    DataList datalist = e.Item.FindControl("dlImages") as DataList;

    //find the correct group id of the item
    string student_Id = DataBinder.Eval(e.Item.DataItem, "Group_Id").ToString();
    SqlDataAdapter sda = new SqlDataAdapter("select pc_name = pc.Name, s_Id = si.Group_Id, si_filename = si.filename, si_description = si.imageDesc from Groups as s inner join dbo.group_images as si on s.Group_Id = si.group_id inner join dbo.photo_collection_images pci on pci.group_image_id = si.Group_Id inner join dbo.photo_collection as pc on pc.id = pci.photo_collection_id where s.Group_Id =" + student_Id, con);
    DataTable dt = new DataTable();
    sda.Fill(dt);

    //bind data to the nested datalist with the Group_Id in the where clause of the query
    datalist.DataSource = dt;
    datalist.DataBind();
}

0 个答案:

没有答案