用C#编写winform 存储图片到sql数据库上的代码是怎么样的 怎么在查询的时候给显示在pictruebox上啊
请各位大哥帮忙解决下啊

解决方案 »

  1.   

    用bitmap 读照片。 转化成二进制流的字符串。然后储存进数据库
      

  2.   

    Stream ms;
      byte[] picbyte;
      OpenFileDialog ofdSelectPic = new OpenFileDialog();
      if (ofdSelectPic.ShowDialog() == DialogResult.OK)
      {
      if ((ms = ofdSelectPic.OpenFile()) != null)
      {
      picbyte = new byte[ms.Length];
      ms.Position = 0;
      ms.Read(picbyte, 0, Convert.ToInt32(ms.Length));
      SqlConnection conn = new SqlConnection();
      conn.ConnectionString = "";  sql = "Insert into Person(Photo) values(@Image)";
      SqlCommand cmd = new SqlCommand(sql, conn);  cmd.Parameters.Add("@Image", SqlDbType.VarBinary);
      cmd.Parameters["@Image"].Value = picbyte;  conn.Open();
      cmd.ExecuteNonQuery();
      conn.Close();  ms.Close();
      }
      } SqlConnection conn=new SqlConnection();   
     conn.ConnectionString="";   
     string strSql="";   
     SqlCommand cmd=new SqlCommand(strSql,conn);   
     conn.Open();   
     SqlDataReader reader=cmd.ExecuteReader();   
      reader.Read();   
     MemoryStream ms=new MemoryStream((byte[])reader["Photo"]);   
     Image image=Image.FromStream(ms,true);   
     reader.Close();   
     conn.Close();   
     picturebox1.Image=image;   
    FileStream mystream=new FileStream("D:\\A.jpg",FileMode.Open,FileAccess.Read);
    long len=mystream.Length;
    mycmd.Parameters.Add("@image",SqlDbType.Image,(int)len,"picture");
    mycmd.Parameters["@image"].Direction=System.Data.ParameterDirection.Input;
    byte []box=new byte[len];   
    mystream.Read(box,0,(int)len);
    mycmd.Parameters["@image"].Value=box;
      

  3.   

    FileStream mystream=new FileStream("D:\\A.jpg",FileMode.Open,FileAccess.Read);
    这里面的路径是什么用的啊  请教
      

  4.   


    private void btnUpload_Click(object sender, EventArgs e)
            {
                openPic.Title = "Choose Picture";
                openPic.Filter = "JPEG(*.jpg)|*.jpg|PNG(*.png)|*.png";            if (openPic.ShowDialog() == DialogResult.OK)
                {
                    if (File.Exists(openPic.FileName))
                    {
                        if (openPic.OpenFile().Length > 1024 * 1024 * 2)
                        {
                            MessageBox.Show("图片不得超过2M");           
                            return;
                        }
                        string image = openPic.FileName;
                        string picpath = openPic.FileName;
                        Guid guid = Guid.NewGuid();
                        this.txtPhoto.Text = openPic.FileName;
                        if (!Directory.Exists(Application.StartupPath + "\\image"))
                        {
                            Directory.CreateDirectory(Application.StartupPath + "\\image");
                        }
                        this.txtPhoto.Tag = Application.StartupPath + "\\image\\" + guid.ToString() + ".jpg";
                        File.Copy(openPic.FileName, Application.StartupPath + "\\image\\" + guid.ToString() + ".jpg");
                        picPhoto.Image = Image.FromFile(Application.StartupPath + "\\image\\" + guid.ToString() + ".jpg");
                        picPhoto.ImageLocation = Application.StartupPath + "\\image\\" + guid.ToString() + ".jpg";
                    }
                    else
                    {
                        MessageBox.Show("图片不能为空");
                        return;
                    }
                }
            }
      

  5.   

    wuyq11 的方法很好,强烈推荐。
      

  6.   

    FileStream mystream=new FileStream("D:\\A.jpg",FileMode.Open,FileAccess.Read);
    能否解释下那个路径是什么意思