private void button2_Click(object sender, EventArgs e)//把要传的图片上传到数据库
        {
             //System.IO.FileStream fs2;
            openFileDialog1.Filter = "(*.jpg)|*.jpg|(*.bmp)|*.bmp|(*.gif)|*.gif";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                
                pictureBox1.ImageLocation =openFileDialog1.FileName;
               // FileStream fs2 = File.OpenRead(pictureBox1.ImageLocation);
                FileStream fs2 = new FileStream(pictureBox1.ImageLocation, FileMode.Open, FileAccess.Read);
                byte[] imagebytes = new byte[fs2.Length];//fs.lenth文件流长度,用字节表示
                BinaryReader br = new BinaryReader(fs2);//二进制读写器
                imagebytes = br.ReadBytes(Convert.ToInt32(fs2.Length));
               // fs2.Read(imagebytes, 0, imagebytes.Length);                SqlConnection con = new SqlConnection("server=(local);Integrated Security=SSPI;database=library");
                con.Open();
                SqlCommand cmd = new SqlCommand("INSERT INTO Pic(Picture)VALUES('imagebytes')", con);
                cmd.ExecuteNonQuery();                MessageBox.Show("添加成功");
                con.Close();
            }
        }
  private void button1_Click(object sender, EventArgs e)//把图片从数据库中读出来
        {            byte[] imagesbytes = null;//where CardID='"+ textBox1.Text.Trim() +"' 
            //string txt = null;
            SqlConnection con = new SqlConnection("server=(local);Integrated Security=SSPI;database=library");
            con.Open();
            SqlCommand com = new SqlCommand("Select Picture from Pic ", con);
            SqlDataReader sdr = com.ExecuteReader();
            //while (sdr.Read())
            //{
            //    //if (sdr.GetValue(0) == null)
            //    //    MessageBox.Show(sdr[0].ToString());
            //    //else
            //        imagesbytes = (byte[])sdr.GetValue(0);
            //    //MessageBox.Show(sdr.GetValue(0).ToString());
            //}
            if (sdr.Read())
            {                byte[] photo = sdr[0] as byte[];//将第0列的数据写入byte数组                FileStream fs = new FileStream(pictureBox2.ImageLocation, FileMode.CreateNew);//创建FileStream对象,用于写入字节数据流                fs.Write(photo, 0, photo.Length);//将byte数组中的数据写入fs
                Bitmap map = new Bitmap(fs);//
                pictureBox2.Image = map;                fs.Close();//关闭fs            }
            sdr.Close();
            //MemoryStream ms = new MemoryStream(imagesbytes);
      
            //Bitmap bmpt = new Bitmap(ms);
            //pictureBox1.Image = bmpt;        }

解决方案 »

  1.   

    SqlCommand cmd = new SqlCommand("INSERT INTO Pic(Picture)VALUES('imagebytes')", con);这样插入到数据库里是固定的'imagebytes'字符串,不是你二进制数组
    ,你改成@传参方式
    SqlCommand cmd = new SqlCommand("INSERT INTO Pic(Picture)VALUES(@imagebytes)", con);
      

  2.   

    这个不论加不加调试时都显示添加成功,但是在把图片从数据库中读取出来是就提示  //Bitmap bmpt = new Bitmap(ms);ms参数无效
      

  3.   

    你存进去'imagebytes'这几个字符串,可不是能成功咋地?
    你要是能把'imagebytes'这几个字符串转成Bitmap,那你就神了
      

  4.   

    其他字符都可以直接用Insert插入,为什么图片转换成二进制数组后数据就不能直接插入而要用到    this.sqlCommand1.Parameters.Add("@img",System.Data.SqlDbType.Image);