在首页piclist.aspx我是这样做的:
<asp:DataList ID="dlContent" runat="server" Width="554px">
<ItemTemplate>
<table cellpadding="0" cellspacing="0">
<tr>
<td style="width: 554px; text-align: left; height: 26px;">
<img id='img1' src='StreamImg.aspx?id= <%# DataBinder.Eval(Container.DataItem,"ImageID") %>'>
</a>
</a>
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>后台是这个piclist.aspx.cspublic partial class Test_Test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//连接数据库
string ConnStr = "Data Source=Localhost; Initial Catalog=EX_NEW; User ID=sa;Pwd=photosystem;";
SqlConnection sqlcon = new SqlConnection(ConnStr);
sqlcon.Open();
string sqlstr = "select ImageID from ImageStore";
SqlDataAdapter MyAdapter = new SqlDataAdapter(sqlstr, sqlcon);
DataSet ds = new DataSet();
MyAdapter.Fill(ds, "ImageData");
this.dlContent.DataSource = ds;
this.dlContent.DataBind();
sqlcon.Close();
}
}
}然后通过StreamImg.aspx.cs绑定数据库里的["ImageID"]public partial class StreamImg : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//string type = Request.QueryString["pt"];
int id = Convert.ToInt32(Request.QueryString["ImageID"]);
Showpic(id);
} private void Show(int id)
{
throw new NotImplementedException();
}
private void Showpic(int id)
{
//连接数据库
string ConnStr = "Data Source=Localhost; Initial Catalog=EX_NEW; User ID=sa;Pwd=photosystem;";
string strSql = "select * from ImageStore where ImageID='" + id + "'";
SqlConnection conn = new SqlConnection(ConnStr);
conn.Open(); SqlCommand cmd = new SqlCommand(strSql, conn);
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
Response.ContentType = "application/octet-stream";
Response.BinaryWrite((Byte[])reader["ImageData"]);
Response.Write("successful");
}
reader.Close();
conn.Close();
Response.End();
}
} 但很不幸,整个代码运行起来,看到的是"红叉",不知道是我那里写错了,寄望大侠们帮帮我...
<asp:DataList ID="dlContent" runat="server" Width="554px">
<ItemTemplate>
<table cellpadding="0" cellspacing="0">
<tr>
<td style="width: 554px; text-align: left; height: 26px;">
<img id='img1' src='StreamImg.aspx?id= <%# DataBinder.Eval(Container.DataItem,"ImageID") %>'>
</a>
</a>
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>后台是这个piclist.aspx.cspublic partial class Test_Test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//连接数据库
string ConnStr = "Data Source=Localhost; Initial Catalog=EX_NEW; User ID=sa;Pwd=photosystem;";
SqlConnection sqlcon = new SqlConnection(ConnStr);
sqlcon.Open();
string sqlstr = "select ImageID from ImageStore";
SqlDataAdapter MyAdapter = new SqlDataAdapter(sqlstr, sqlcon);
DataSet ds = new DataSet();
MyAdapter.Fill(ds, "ImageData");
this.dlContent.DataSource = ds;
this.dlContent.DataBind();
sqlcon.Close();
}
}
}然后通过StreamImg.aspx.cs绑定数据库里的["ImageID"]public partial class StreamImg : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//string type = Request.QueryString["pt"];
int id = Convert.ToInt32(Request.QueryString["ImageID"]);
Showpic(id);
} private void Show(int id)
{
throw new NotImplementedException();
}
private void Showpic(int id)
{
//连接数据库
string ConnStr = "Data Source=Localhost; Initial Catalog=EX_NEW; User ID=sa;Pwd=photosystem;";
string strSql = "select * from ImageStore where ImageID='" + id + "'";
SqlConnection conn = new SqlConnection(ConnStr);
conn.Open(); SqlCommand cmd = new SqlCommand(strSql, conn);
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
Response.ContentType = "application/octet-stream";
Response.BinaryWrite((Byte[])reader["ImageData"]);
Response.Write("successful");
}
reader.Close();
conn.Close();
Response.End();
}
} 但很不幸,整个代码运行起来,看到的是"红叉",不知道是我那里写错了,寄望大侠们帮帮我...
在Page_load里读取数据库,然后把读出来的byte[]用Response.Write()写进流里。dL里绑定图片的imageurl=那个img.aspx
在 while (reader.Read()) 加断点
看看是否读到数据