现在数据库有个表 tb_name,有字段 title,pic,id 其中 pic 是 image类型,id是自增标识字段。现在建立了一个 showtable.aspx 里面放了一个 gridview,在gridview我添加了两个列,一个是 title,一个是 pic,pic是 imagefield字段,我设置了 DataImageUrlField 为 id ,DataImageUrlFormatString 为 showpic.aspx?id={0}showpic.aspx.cs 代码如下:
public partial class showpic : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {  //连接数据库 
        string ConnStr = "server=192.168.1.101;user id=sa;pwd=;database=dbname";
        string strSql = "select * from tb_name where id=" + id;
        SqlConnection conn = new SqlConnection(ConnStr);
        conn.Open();        SqlCommand cmd = new SqlCommand(strSql, conn);
        SqlDataReader reader = cmd.ExecuteReader();        if (reader.Read())
        {
            Response.ContentType = "application/octet-stream";
            Response.BinaryWrite((Byte[])reader["pic"]);        }
        reader.Close();
        conn.Close();
        Response.End();
    }不知道有什么错误,现在就是 显示不出图片来,搞了快一天了,哪些大虾给弄下啊!谢谢了

解决方案 »

  1.   

      if (reader.Read())
      {
      Response.ContentType = "application/octet-stream";
      if(reader["pic"]!=Null)
      {
      Response.BinaryWrite((Byte[])reader["pic"]);
      }
      }
      

  2.   

    http://www.codeproject.com/aspnet/PicManager.asp
      

  3.   

    我还以为是WIN程序呢WEB的只能帮项下了
      

  4.   


    MemoryStream stream = new MemoryStream ();
        SqlConnection connection = new 
          SqlConnection (@"server=INDIA\INDIA;database=iSense;uid=sa;pwd=india");
        try
        {
            connection.Open ();
            SqlCommand command = new 
              SqlCommand ("select Picture from Image", connection);
            byte[] image = (byte[]) command.ExecuteScalar ();   
            stream.Write (image, 0, image.Length);
            Bitmap bitmap = new Bitmap (stream);
            Response.ContentType = "image/gif";
            bitmap.Save (Response.OutputStream, ImageFormat.Gif);
        } 
        finally
        {
            connection.Close ();
            stream.Close ();
        }
    在IE里只访问showpic.aspx?id={0}这个页面看看图片能不能正常显示