搞了一天了,写入的和读取出来的数组总是对不上号,实在不知道怎么办了,求大婶们帮忙写个简单的范例。全分送上了~
数据库为SQL server.
储存类型为image。
写入的数组为byte[] aaa =new byte[1024].
读取的数组为byte[] bbb.
 不胜感激

解决方案 »

  1.   

                SqlConnection conn = new SqlConnection("server=.;uid=sa;pwd=;database=s");
                conn.Open();
                string strSql = "select image from table ";
                SqlCommand cmd = new SqlCommand(strSql, conn);
                SqlDataReader sdr = cmd.ExecuteReader();
                sdr.Read();
                MemoryStream ms = new MemoryStream((byte[])sdr[0]);
                Image image = Image.FromStream(ms);
                sdr.Close();
                conn.Close();
                pictureBox1.Image = image;参考http://www.cnblogs.com/zqn518/archive/2012/04/28/2475664.html
      

  2.   

    哥们,这都是转换图片的,我不想搞图片,就是把一个byte[]存入和读取,我现在重点是读取和写入不正确,所以我想要具体写入和读取这两快的代码
      

  3.   

    SqlConnection conn = new SqlConnection("server=.;uid=sa;pwd=;database=s");
                conn.Open();
                string strSql = "select image from table ";
                SqlCommand cmd = new SqlCommand(strSql, conn);
                SqlDataReader sdr = cmd.ExecuteReader();
                sdr.Read();
                MemoryStream ms = new MemoryStream((byte[])sdr[0]);
                Image image = Image.FromStream(ms);
                sdr.Close();
                conn.Close();
                pictureBox1.Image = image;
      

  4.   

    我也曾经遇到过这个问题,插入和读取出来的二进制不一样,我想你很可能是因为你的插入代码写的有失误。这里给你个范例,写入和读出
    //写入:
    string stringcon = ConfigurationManager.ConnectionStrings["Connectionstring"].ConnectionString;
    SqlConnection Con = new SqlConnection(stringcon);
    String SqlCmd = "INSERT INTO indexsql(Image,, ImageSize)values(@Image,@ImageSize)";//indexsql为数据表,改成你的
    //出现写入和读出数据不一致的原因有可能你的SQL语句为insert into indexsql image='aaa';直接给赋值了
    SqlCommand CmdObj = new SqlCommand(SqlCmd, Con);
     Con.Open();
     CmdObj.Parameters.Add("@Image", SqlDbType.Image);
     CmdObj.Parameters.Add("@ImageSize", SqlDbType.Int);
     CmdObj.Parameters["@Image"].Value = aaa;//这里为你的数组,即图片
     CmdObj.Parameters["@ImageSize"].Value = UpFile.ContentLength; //图片的大小,upFile为上传控件
                    CmdObj.ExecuteNonQuery();//读出:
    int ImgID = Convert.ToInt32(Request.QueryString["id"]); 
    string stringcon = ConfigurationManager.ConnectionStrings["Connectionstring"].ConnectionString;
    SqlConnection Con = new SqlConnection(stringcon );
    String SqlCmd = "select * from indexsql where id="+ImgID ;
    SqlCommand CmdObj = new SqlCommand(SqlCmd, Con);
    Con.Open();
    SqlDataReader SqlReader = CmdObj.ExecuteReader();
    SqlReader.Read();                 
    Response.OutputStream.Write  ((byte[])SqlReader["Image"], 0, (int)SqlReader["ImageSize"]);//读出并显示 读取的数组 byte[] bbb= (byte[])SqlReader["Image"];  
    Response.End();
    Con.Close();               }
    这样应该就可以了,楼主给分吧!