从数据库中读取以二进制存储的图片信息问题
首先,将图片信息转成二进制存到数据库中,具体程序如下:
protected void btnSend_Click(object sender, EventArgs e)
{
string bookid = txtName.Text;
string picname = fupPicturePath.FileName; //获取文件名
string lastName = picname.Substring(picname.LastIndexOf(".") + 1); //获取文件后缀名
SqlConnection sqlconn = new SqlConnection(Constring);
sqlconn.Open();
//判断上传文件的格式
if (lastName.ToLower() == "bmp" || lastName.ToLower() == "jpg" || lastName.ToLower() == "gif")
{
int len = fupPicturePath.PostedFile.ContentLength; //获取文件的大小
Byte[] pictureData = new Byte[len];
HttpPostedFile htp = fupPicturePath.PostedFile;
Stream srm = htp.InputStream;
srm.Read(pictureData, 0, len); //读取到pictureData中
SqlCommand mycmd = new SqlCommand("update tb_Book set CoverPic='"+ @pictureData +"',pictureName='"+picname+"'where BookId='"+txtName.Text+"'",sqlconn);
mycmd.Parameters.Add("@pictureData", SqlDbType.Image); //指定类型
mycmd.Parameters["@pictureData"].Value = pictureData; //设置参数值
mycmd.ExecuteNonQuery();
gvBind(); //重新绑定GridView
sqlconn.Close();
}
else
RegisterStartupScript("false", "<script>alert('图片格式不正确!!')</script>");
}
然后,我在另外一个页面中,读取已经存储的图片信息,具体代码如下:
SqlConnection conn = new SqlConnection(Constring);
conn.Open(); //打开数据库连接
string bid = Request.QueryString["bookid"];
string sqlcon = "select * from tb_Book where BookId='" + bid + "'";
SqlCommand com = new SqlCommand(sqlcon, conn);
SqlDataReader sdr = com.ExecuteReader();
sdr.Read(); //读取数据
//将二进制写入到输出流
Response.BinaryWrite((Byte[])sdr["CoverPic"]);
sdr.Close();
conn.Close();
现在问题是,我有一个image服务器控件,如何将输出的图片信息显示出来,现在运行后,页面上已经显示出System.Byte[]