c# - 通过外键获取图像

标签 c# .net asp.net

我有一个表,其中包含有关产品照片的数据,该表与产品表相关,并且产品可能比图像更多。我很好地完成了我的代码,通过 productID 查询字符串获取产品的所有图像,但只检索到第一个图像。请注意,处理程序页面中的 ProductIDPhotobank 图像表中的外键, 我还注意到 productID 具有 ID 表的值,但 productID 没有:

存储过程:

ALTER proc [dbo].[SP_GetPhotoBank]
(
@ProductID Int 
)
as
SELECT     ID, ProductID, DesignImage
FROM       ProductImages
where      ProductID=@ProductID

标记:

<div>
    <asp:DataList ID="DLProduct" runat="server" OnItemDataBound="DLProduct_ItemDataBound">
        <ItemTemplate>
            <table style="width: 100%;">
                <tr>
                    <td>
                        <asp:Label ID="LblModel" runat="server" Text='<%#Eval("Model") %>' Style="color: #CC3300"></asp:Label>
                    </td>
                    <td>
                        &nbsp;
                    </td>
                </tr>
                <tr>
                    <td colspan="2">
                                                </td>
                </tr>
                <tr>
                    <td>
                        <asp:Image ID="IMGPhotoBank" runat="server" ImageUrl='<%#Eval("ProductID","~/Handlers/PhotoBankByProductID.ashx?ProductID={0}") %>' />
                    </td>
                   </tr>
               </table>
           </ItemTemplate>
       </asp:DataList>
   </div>

代码:

public void ProcessRequest(HttpContext context)
{
    using (SqlConnection con = Connection.GetConnection())
    {           
        SqlCommand com = new SqlCommand("SP_GetPhotoBank", con);
        com.CommandType = System.Data.CommandType.StoredProcedure;
        com.Parameters.Add(Parameter.NewInt("@ProductID", context.Request.QueryString["ProductID"].ToString()));
        SqlDataReader dr = com.ExecuteReader();
        if (dr.Read() && dr != null)
        {
            Byte[] bytes2 = (Byte[])dr["DesignImage"];
            context.Response.BinaryWrite(bytes2);
            dr.Close();
        }
    }
}

最佳答案

我认为您需要循环访问数据读取器中的数据,因为目前您只能读取第一个结果,然后关闭数据读取器。

while(dr.Read())
{
    // add results to some collection
}

// close the reader and tidy up
dr.Close();
dr.Dispose();

// now render your data 

关于c# - 通过外键获取图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5389319/

相关文章:

c# - 从 C# 调用 ILAsm vararg 函数

c# - 不同地区不同认证方式

c# - 在 Controller 上下文之外获取 View

c# - 如何检测当前按下的键?

c# - 使用 CaSTLe 单例会导致 ASP.NET Web 应用程序中的争用

asp.net - 重定向不适用于移动 View asp.net

c# - Azure 表存储插入或合并

.net - VB.NET 可空值

asp.net - 如何在 ASP.NET 中的 gridview 中为每个标题列添加标题

c# - 我可以使用哪种用户信息来创建唯一的用户 key ?