我是这样保存的
byte[] fileByte = null;
fileByte = new byte[files[0].ContentLength];
数据库里面是Image类型,现在我要读取出来
public  byte [] WJL
{
     get { return _wjl; }
     set { _wjl = value; }
} //WJL = Convert.ToByte( dr["WJL"].ToString());  这里应该怎么写啊?

解决方案 »

  1.   

    http://www.2cto.com/kf/201201/117016.html
      

  2.   

     protected void Page_Load(object sender, EventArgs e) 
    {
                string id = Request.Params["Id"];
              
                DataSet ds = service.getimage(id);
                if (ds.Tables[0].Rows.Count == 0)
                    return;
                byte[] bydata = (byte[])ds.Tables[0].Rows[0][0];            Response.ClearContent();
                Response.ContentType = "image/jpeg";
                Response.BinaryWrite(bydata);
    }
      

  3.   

    /// <summary>
            /// 从数据库读取相应图片
            /// </summary>
            /// <param name="name">图片名称</param>
            /// <returns>返回该图片</returns>
            public static Image GetImage(string name)
            {
                SqlConnection con = new SqlConnection(SqlConString);
                SqlCommand cmd = con.CreateCommand();
                SqlParameter pname = new SqlParameter("@name", name);
                cmd.Parameters.Add(pname);
                cmd.CommandText = "select image from MyTable where name=@name";
                con.Open();
                byte[] MyData = (byte[])cmd.ExecuteScalar();
                MemoryStream ms = new MemoryStream(MyData, 0, MyData.Length);
                ms.Seek(0, SeekOrigin.Begin);
                Image image = Image.FromStream(ms);
                con.Close();
                return image;
            }
      

  4.   

    byte[] fileByte = null;
    fileByte = new byte[files[0].ContentLength];
    privateAch.WJL = fileByte;
    FileInfo fi = new FileInfo(file.FileName.ToString());我这样写的,保存到数据库里的的值是"0x0后面全是0"这样的值是对的吗?现在我下载后文件报错说”损坏“
      

  5.   

    files[0].ContentLength这个是内容吧。
    byte[] data=system.io.file.read();
      

  6.   

    我想说的是,我这样写上传对还是不对,不对该怎么写啊?byte[] fileByte = null;
    fileByte = new byte[files[0].ContentLength];
      

  7.   

    你是想把文件的路径放到数据库中吧?这样显示是不行的,只是创建了文件长度大小的字节数组。
    另外数据库中这个字段是image类型,那就说明要把文件存到数据库吧,
    1.你是要把文件存到数据库,还是文件名?
    2.上边的这点代码如果是把文件存到数据库,第一步数组初始化没有问题,但是这个字节数据没有赋值