我想從數據庫中檢索具有圖像類型列的圖像實際上我從一張桌子有表我想顯示圖像並從另一張桌子我想要檢索其他字段我正在使用窗體視圖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