我有一个带有image
列的SQL Server表。我已将图像存储为二进制数据,我想检索此图像并使用Image
将其显示在ImageUrl
控件中。
通用处理程序代码:
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
SqlCommand myCommand = new SqlCommand("select admin_image from admin where Id=" +context.Request.QueryString["imageid"]+"", con);
SqlDataReader myReader = myCommand.ExecuteReader();
while (myReader.Read())
{
context.Response.ContentType = "image/jpg";
context.Response.BinaryWrite((byte[])myReader["admin_image"]);
}
在页面加载
上调用泛型处理程序image.ImageUrl = "Handler1.ashx?imageid=" + Request.Cookies["login"]["login_id"]; //id of the user in SQL table from which we want to retrieve the image
代码编译没有问题,但我似乎没有得到图像加载。我发送的id
是正确的。