如何在一个页面中批量显示数据库中的图片?

来源:百度知道 编辑:UC知道 时间:2024/06/17 04:36:07
数据库中存放图片的表有imgid 和 imgdata两个属性,表中存放有很多图片,我想在一个页面中显示几张图片,像相册一样。现在我只能显示一张图片。显示代码如下:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source=.;Initial Catalog=student;Persist Security Info=True;User ID=sa;Password=sa";
int i;

string strSql = "select imgdata from image where imgid=5";//这里假设获取id为5的图片
SqlCommand cmd = new SqlCommand(strSql, conn);
conn.Open();
SqlDataReader reader = cmd.ExecuteReader();

while (reader.Read())
{
//读取

Response.ContentType = "application/octet-stream";
Response.BinaryWrite((Byte[])reader["imgdata&quo

ImageUrl="~/showph.aspx?imgid=X" //X表示imgid的具体值

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source=.;Initial Catalog=student;Persist Security Info=True;User ID=sa;Password=sa";

string strSql = "select imgdata from image where imgid=" + Request.QueryString["imgid"];//这里获取图片
SqlCommand cmd = new SqlCommand(strSql, conn);
conn.Open();
Object oImg = cmd.ExecuteScalar();

//读取

Response.ContentType = "image/*";
Response.BinaryWrite((Byte[])oImg);
Response.End();

}

}

可以用GridView控件就很容易实现把
图片放到数据库中不合适~把图片的路径放到数据库里.
然后把在GridView控件绑定路径