从数据库里读图片出来只能显示System.Byte[]??
代码
string ImgID = Request.QueryString["Portfolio_ID"];
        SqlConnection MyConnection = new SqlConnection("Server=.;Uid=sa;Pwd=;DataBase=Portfolio_System");
        SqlCommand MyCmd = new SqlCommand("Select * From Portfolio Where Portfolio_ID='"+ImgID+"'",MyConnection);
        MyConnection.Open();
        SqlDataReader ODR = MyCmd.ExecuteReader();
        if (ODR.Read())
        {
            Response.ContentType = ODR["ImgType"].ToString();
            Response.BinaryWrite((Byte[])ODR["Img"]);
        }
        MyConnection.Close();
    }

解决方案 »

  1.   

    不太清楚,记得以前写时byte是小写的啊
      

  2.   

    DataSet ds=new DataSet();
    ds = dbs.table_Select(sql);
    Response.ContentType = "image/jpeg";
    Response.BinaryWrite((byte[])ds.Tables[0].Rows[0][0]);我以前这么些可以的。试试阿
      

  3.   

    private void Button1_Click(object sender, System.EventArgs e)
    {
    System.Web.HttpPostedFile upfile=this.File1.PostedFile;
    int length=upfile.ContentLength;
    try
    {
    if(length==0)
    {
    this.Label1.Text="请选择您上传的文件";
    }
    else
    {
    byte[] fileByteArr=new byte[length];
    Stream streamObject =upfile.InputStream;
    streamObject.Read(fileByteArr,0,length);
    string strCon="server=localhost;uid=sa;password=123;database=northwind";
    SqlConnection cn= new  SqlConnection(strCon);
    string sql="insert into images(data,contenttype,description,imagesize) values(@data,'{0}','{1}','{2}')";
    string sqlInsert=string.Format(sql,upfile.ContentType,this.TextBox1.Text.ToString(),length);

    SqlCommand cmd=new SqlCommand(sqlInsert,cn);
    cmd.Parameters.Add("@data",System.Data.SqlDbType.Image);
    cmd.Parameters["@data"].Value=fileByteArr;
    cmd.Connection.Open();
    cmd.ExecuteNonQuery();
    cmd.Connection.Close();
    this.Label1.Text="上传成功";
    }
    }
    catch(Exception ex)
    {
    this.Label1.Text=ex.Message.ToString(); }
    } private void Button2_Click(object sender, System.EventArgs e)
    {
    string strCon="server=localhost;uid=sa;password=123;database=northwind";
    SqlConnection cn= new  SqlConnection(strCon);
    string sql="select * from images where id=4";
    SqlCommand cmd=new SqlCommand(sql,cn);
    cmd.Connection.Open();
    SqlDataReader dr=cmd.ExecuteReader();
    if(dr.Read())
    {
    Response.ContentType=dr["contenttype"].ToString();
    Response.OutputStream.Write((byte[])dr["data"],0,(int)dr["imagesize"]);
    }
    dr.Close();
    cmd.Connection.Close();

    }这是完整的上传图片到sql和显示图片的例子
    Response.OutputStream.Write((byte[])dr["data"],0,(int)dr["imagesize"]);
    输出要这么写吧
      

  4.   

    我用的是Access数据库,存放图片用的是Ole对象..
    用你的方法,读取的时候提示指定的转换无效...
    行:Response.OutputStream.Write((byte[])ODR["ImageData"],0,(int)ODR["Image_Size"]);
      

  5.   

    晕了...换成Sql Server的也是一样,难道2.0和1.1不同???
      

  6.   

    string str=System.Configuration.ConfigurationSettings.AppSettings["cn"];
    SqlConnection cn=new SqlConnection(str);
    SqlCommand cmd=new SqlCommand();
    cmd.CommandText="select Cover from Books where BookGuid='"+this.Request["ImageID"]+"'";
    cmd.Connection=cn;
    cn.Open();
    this.Response.ContentType="image/*";
    SqlDataReader dr=cmd.ExecuteReader();
    while(dr.Read())
    {
    this.Response.BinaryWrite((byte[])dr["Cover"]);
    }
    cn.Close();