我想从数据库中检索具有图像类型列的图像实际上我从一张桌子有表我想显示图像并从另一张桌子我想要检索其他字段我正在使用窗体视图http处理程序不显示图像
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();
}
的HttpHandler:
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;
}
}
它显示一个表中的字段,但不显示来自另一个表的图像。 。 我在哪里出错? 您的帮助将不胜感激。 。 .Thanx
首先,不要在SQL不要使用字符串连接查询 –
@kostasch:好的,但是请你告诉我,为什么它不以dB为单位工作的imageContent列 – user2517610
检查大小。 – mck