借鉴网上写了一段从库中读取图片字节流,写到页面的代码,请大家帮忙看看有没有问题,有没有需要优化的地方。
谢了! string SchoolID = Request.QueryString.Get("SchoolID") != null ? Request.QueryString.Get("SchoolID").ToString() : "0";
SqlConnection cn = new SqlConnection(tblBase.GetConnectionString());
SqlCommand cm = new SqlCommand("select SchoolID,SchoolLogo from dbo.tblSchoolLogos where SchoolID=" + SchoolID, cn);
cn.Open();
SqlDataReader dr = cm.ExecuteReader();
if (dr.Read())
{
Response.BinaryWrite((byte[])dr["SchoolLogo"]);
}
Response.End(); dr.Close();
cm.Clone();
cn.Close();
谢了! string SchoolID = Request.QueryString.Get("SchoolID") != null ? Request.QueryString.Get("SchoolID").ToString() : "0";
SqlConnection cn = new SqlConnection(tblBase.GetConnectionString());
SqlCommand cm = new SqlCommand("select SchoolID,SchoolLogo from dbo.tblSchoolLogos where SchoolID=" + SchoolID, cn);
cn.Open();
SqlDataReader dr = cm.ExecuteReader();
if (dr.Read())
{
Response.BinaryWrite((byte[])dr["SchoolLogo"]);
}
Response.End(); dr.Close();
cm.Clone();
cn.Close();
/或byte[] imageData = (byte[])dr[2];
MemoryStream ms = new MemoryStream(sb.Value);//在内存中操作图片数据
Bitmap bmp = new Bitmap(Bitmap.FromStream(ms));
this.pictureBox1.Image = bmp;
dr.Close();直接显示到pictureBox1里面。Bs的一样显示image.url=
SqlCommand cm = new SqlCommand("select SchoolID,SchoolLogo from dbo.tblSchoolLogos where SchoolID=" + SchoolID, cn);不要用拼接字符串了~
是不是哪个地方没有释放