【发布时间】:2014-04-22 11:52:50
【问题描述】:
我在 asp.net 中工作,我想显示 Profile_ID 为 1 的用户的图片。我直接将图片以 varbinary 格式保存到数据库中。并在gridview中检索它。我面临的问题是,当用户登录并单击“查看照片”按钮时,如果用户上传了 4 张不同的图片,则同一张图片会出现四次。它没有拍摄用户上传的所有四张照片。我正在使用处理程序进行操作。
这是我的表格规格: 表名:(照片) 列:Photo_ID、Profile_ID(外键)、照片
下面是我的 GridView 代码
<asp:Image ID="Image1" runat="server" ImageUrl='<%# "ThumbNail.ashx?proid="+ Eval("Profile_ID") %>' Height="200px" Width="200px"/>
这就是我将数据绑定到网格的方式
SqlConnection connection = new SqlConnection(strCon);
SqlCommand command = new SqlCommand("SELECT Photo_ID,Profile_ID from [Photo] where Profile_ID=" +Session["profileid"], connection);
SqlDataAdapter daimages = new SqlDataAdapter(command);
DataTable dt = new DataTable();
daimages.Fill(dt);
gridview.DataSource = dt;
gridview.DataBind();
这是处理程序的编码,我正在执行检索图像的主要工作
string pid = context.Request.QueryString["proid"];
string query = "select Photo from Photo where Profile_ID=" + pid;
SqlDataAdapter adpt = new SqlDataAdapter(query, connection);
DataTable dt = new DataTable();
adpt.Fill(dt);
for (int i = 0; i < dt.Rows.Count; i++)
{
context.Response.BinaryWrite((byte[])(dt.Rows[i]["Photo"]));
}
【问题讨论】: