小弟向各位大虾求一段图片输入的代码,我要做一个人员管理系统采用CS模式,其中有一项是显示员工的头像的图片,小弟由于手生不知道怎么处里了,这个图片我想当用户点击插入图片按钮时,在机器上选中图片然后图片以二进制流的形式保存倒数据库的表中,然后每次在查看员工资料的时候有员工照片的资历页可以自动显示出员工的照片。请各位大侠帮我!感激不尽!!!!

解决方案 »

  1.   

    给你个例子,有些自己改改:
    保存:
    try
    {
    conn=new SqlConnection (connectString );
    conn.Open ();
    com=new SqlCommand (sql,conn);
    sdr=null;
    if(conn.State .ToString ()=="Closed")
    {
    conn.Open() ;
    }
    sdr=com.ExecuteReader ();
    if(sdr!=null)
    {
    while(sdr.Read ())
    {
    byte[] MyData = (byte[])dr["image"];
                        this.pictureBox1.Image = Image.FromStream(new MemoryStream(MyData));     }
    sdr.Close ();
    }
    conn.Close ();

    }
    catch(Exception ex)
    {
                    MessageBox.Show(ex.Message);
    };
      

  2.   

    错了,上面读出来.
    下面的才是保存:
     try
                    {
                        SqlConnection conn = new SqlConnection(connectString );
                        string sql = "select top 5 * from question";
                        SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
                        SqlCommandBuilder MyCB = new SqlCommandBuilder(sda);
                        DataSet ds = new DataSet("image");                    FileStream fs = new FileStream(PicturePath, FileMode.OpenOrCreate, FileAccess.Read);//PicturePath是图片的路径
                        byte[] MyData = new byte[fs.Length];
                        fs.Read(MyData, 0, System.Convert.ToInt32(fs.Length));
                        fs.Close();
                        sda.Fill(ds, "image");                    DataRow myRow;
                        myRow = ds.Tables["image"].NewRow();                        
                        myRow["image"] = MyData;                    ds.Tables["image"].Rows.Add(myRow);
                        sda.Update(ds, "image");
                        conn.Close();
                      }
                    catch (Exception Ex)
                    {
                        MessageBox.Show(Ex.ToString());
                    }
      

  3.   

    this.pictureBox1.Image = Image.FromStream(new MemoryStream(MyData));    这句话报{"使用了无效参数。" }的错误,请问是怎么回事啊