我正在尝试从数据库中检索具有图像类型列的图像实际上我有一个表中的表我想显示图像并且从另一个表中我想检索我正在使用表单视图的其他字段
.aspx 文件
<asp:FormView runat="server" ID="ListStories" DefaultMode="ReadOnly" >
<ItemTemplate>
<table>
<tr><td><%#Eval("Subject") %></td></tr>
<tr><td><%#Eval("Story") %></td></tr>
<tr><td><%#Eval("UserName")%> <asp:Image ID="Image1" runat="server" ImageUrl='~/ShowImage.ashx?Name=<%#Eval("UserName") %>' Width="150" Height="150" /></td></tr>
</table>
</ItemTemplate>
</asp:FormView>
后面的代码:
string connString = ConfigurationManager.ConnectionStrings["Alumnidb"].ConnectionString;
SqlConnection conn;
SqlCommand cmdStories;
SqlDataReader reader;
protected void Page_Load(object sender, EventArgs e)
{
RetriveStories();
}
protected void RetriveStories()
{
conn = new SqlConnection(connString);
//cmdStories = new SqlCommand("SELECT Stories.UserName, Stories.Subject, Stories.Story, UserProfile.Photo FROM Stories INNER JOIN UserProfile ON UserProfile.UserName=Stories.UserName", conn);
cmdStories = new SqlCommand("SELECT UserName, Subject, Story FROM Stories",conn);
conn.Open();
reader = cmdStories.ExecuteReader();
ListStories.DataSource = reader;
ListStories.DataBind();
conn.Close();
}
Http处理程序:
public void ProcessRequest (HttpContext context) {
byte[] buffer = null;
string querySqlStr = "";
if (context.Request.QueryString["Name"] != null)
{
querySqlStr = "SELECT Photo from UserProfile where UserName=" + context.Request.QueryString["Name"];
}
conn = new SqlConnection(connString);
SqlCommand command = new SqlCommand(querySqlStr, conn);
SqlDataReader reader = null;
try
{
conn.Open();
reader = command.ExecuteReader();
//get the extension name of image
while (reader.Read())
{
string name = reader["Photo"].ToString();
int endIndex = name.LastIndexOf('.');
string extensionName = name.Remove(0, endIndex + 1);
buffer = (byte[])reader["imageContent"];
context.Response.Clear();
context.Response.ContentType = "image/" + extensionName;
context.Response.BinaryWrite(buffer);
context.Response.Flush();
context.Response.Close();
}
reader.Close();
}
finally
{
conn.Close();
}
}
public bool IsReusable
{
get
{
return false;
}
}
它显示一个表中的字段,但不显示另一个表中的图像。. 我哪里错了?您的帮助将不胜感激。. .谢谢