【问题标题】:Subquery returning more than 1 value when trying to display images尝试显示图像时子查询返回超过 1 个值
【发布时间】:2017-02-22 16:34:02
【问题描述】:

我正在尝试创建一个照片库页面。 此页面将显示多个组(网球、高尔夫等)。每个组可以有多个照片集,每个集合可以有多个图像。

我正在使用嵌套转发器,首先显示组名称,然后是照片集名称,然后是图像。

如果我的收藏中有一张图片,我的代码就可以正常工作。但是,当我向集合中添加第二张图片时,我在 C# 中收到以下错误,我已在下面发布:

子查询返回超过 1 个值。当子查询跟随 =、!=、、>= 或子查询用作表达式时,这是不允许的

此查询的目的是检索与该照片集相关的所有图像。

我需要进行哪些更改才能显示每个集合的所有图像,而不仅仅是一个?

 <div class="panel-heading">
                                <h4 class="panel-title"><%# Eval("Group_Name") %></h4>
                            </div>
                            <div class="panel-body">
                                <asp:Repeater ID="repPhotoAlbums" runat="server" OnItemDataBound="repPhotoAlbums_ItemDataBound">
                                    <ItemTemplate>
                                        <div class="col-lg-12">
                                            <h5><%# Eval("Name") %></h5>
                                            <asp:Repeater ID="repPhotos" runat="server">
                                                <ItemTemplate>
                                                    <a id="imageLink" href='<%# Eval("filename","../Images/{0}") %>' title='<%#Eval("imageDesc") %>' rel="lightbox[Brussels]">
                                                        <asp:Image ID="Image1" ImageUrl='<%# Bind("filename", "../Images/{0}") %>' runat="server" Width="112" Height="84" />
                                                    </a>
                                                </ItemTemplate>
                                            </asp:Repeater>
                                        </div>
                                        <!--col-lg-12-->
                                    </ItemTemplate>
                                </asp:Repeater>
                            </div>
                            <!--panel-body-->

C#:

   protected void repPhotoAlbums_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
     if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        Repeater repPhotos = (Repeater)(e.Item.FindControl("repPhotos"));


        string Group_Id = DataBinder.Eval(e.Item.DataItem, "p_Collection_Id").ToString();
        //Need to assign the Data in datatable
        SqlDataAdapter sda = new SqlDataAdapter("SELECT Id, filename, imageDesc FROM Group_Images WHERE ID = (Select g_Image_Id = Group_Image_Id FROM Photo_Collection_Images WHERE Photo_Collection_Id = '" + Group_Id + "');", con);
        DataTable dt = new DataTable();
        sda.Fill(dt);
        repPhotos.DataSource = dt;
        repPhotos.DataBind();
    }

}

另外,如果有帮助,我可以发布存储这些数据的 SQL 表。 非常感谢任何可以帮助我的人。

【问题讨论】:

    标签: c# sql-server


    【解决方案1】:

    使用exists():

    select 
       Id
     , filename
     , imageDesc
    from Group_Images as g
    where exists (
      select 1 
      from Photo_Collection_Images as i
      where g.Id = i.Group_Image_Id
        and i.Photo_Collection_Id = '" + Group_Id + "'
      )
    

    注意:实际使用时,这类代码需要使用参数来防止sql injection

    【讨论】:

    • 这应该是参数化的。
    • @SeanLange 我同意,但我猜这是课程作业,他们还没有走到那一步。
    • 可能是这样。我只是说需要提及,因为其他任何事情都是疏忽的。可能还有其他人在这里偶然发现答案并在他们的网站上喷溅。
    • 我认为“飞溅”的选择特别感人。非常好!已添加注释。
    • @SqlZim 嗨,我遇到了与此代码类似的问题:@"DECLARE @Group_Id INT; DECLARE @Another_Id INT; SELECT @Group_Id = Id FROM dbo.Photo_Collection WHERE Id @Id SELECT @Another_Id = Group_Image_Id FROM Photo_Collection_Images WHERE Photo_Collection_Id = @Group_Id SELECT * FROM Group_Images WHERE ID = @Another_Id;"
    【解决方案2】:

    使用IN。另外,使用参数化语句而不是字符串连接:

    select Id,
        filename,
        imageDesc
    from Group_Images
    where ID in (
            select Group_Image_Id
            from Photo_Collection_Images
            where Photo_Collection_Id = @Group_id
            )
    

    或使用加入:

    select i.Id,
        i.filename,
        i.imageDesc
    from Group_Images i
    join Photo_Collection_Images c on i.ID = c.Group_Image_Id
    where c.Photo_Collection_Id = @Group_id
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-11-12
      • 1970-01-01
      • 2013-05-23
      • 2016-04-10
      相关资源
      最近更新 更多