protected void Button1_Click(object sender, EventArgs e)
    {
        if (FileUpload1.HasFile)
        {
            Stream mystream = FileUpload1.PostedFile.InputStream;//FileUpload1是上传控件..
            int length = FileUpload1.PostedFile.ContentLength;
            
            byte[] bytes = new byte[length];
            mystream.Read(bytes, 0, length);
            mystream.Close();
            string conn = "server=(local);database=Image;Uid=sa;Pwd=123456";
            SqlConnection myconn = new SqlConnection(conn);
            myconn.Open();
            string str="insert into image (img) values('" + bytes + "')";
            SqlCommand mycomm = new SqlCommand(str, myconn);
            mycomm.ExecuteNonQuery();
            myconn.Close();
        }
    }
上面把图片存入数据库...
下面是把图片从数据库中读出来...

    protected void Button2_Click(object sender, EventArgs e)
    {
        string imgtype = FileUpload1.PostedFile.ContentType;
        string conn = "server=(local);database=Image;Uid=sa;Pwd=123456";
        string str="select img from image where ID=5";
        SqlConnection myconn = new SqlConnection(conn);
        myconn.Open();
        //SqlDataAdapter myda = new SqlDataAdapter(str, conn);
        //DataSet myds =new DataSet();
        //myda.Fill(myds);
        SqlCommand mycom = new SqlCommand(str, myconn);
        SqlDataReader mydr = mycom.ExecuteReader();
       if (mydr.Read())
        {
            Response.ContentType = imgtype;
            Response.BinaryWrite((byte[])mydr["img"]);
        }
        else
        {
            
            Response.Write("没有从数据库中读取图片");
        }
        myconn.Close();
    }
为什么我的页面上只显示System.Byte[] 

解决方案 »

  1.   

    先用mydr.GetBytes得到一个byte[]数组试试
      

  2.   


     string   imgtype   =   FileUpload1.PostedFile.ContentType; 
    LZ这个地方的FileUpload1中还有值么,如果没有值,你如何能取的到其图片类型呢.
    一般来说,我们都是在把图片二进制流写入数据库的同时把图片的格式也同时写入数据库
    到时候直取读取.
      

  3.   

    我想应该不是楼主说的问题吧......我也试过直接给它定义类型...Response.ContentType   = "image/Gif";//我存的图片都是GIF的...
      

  4.   

    这样试试byte[] b=System.Text.ASCIIEncoding.ASCII.GetBytes(mydr[ "img "].ToString());
    Response.BinaryWrite(b);
      

  5.   

    按照6楼的那样..读出来的也System.byte[]