我有一个表,其中包含有关产品照片的数据,该表与产品表相关,并且产品可能比图像更多。我很好地完成了我的代码,通过 productID
查询字符串获取产品的所有图像,但只检索到第一个图像。请注意,处理程序页面中的 ProductID
是 Photobank
图像表中的外键,
我还注意到 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>
</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/