using(SqlConnection   conn=new   SqlConnection(""))
{     
  conn.Open();     
  SqlCommand   cmd=new   SqlCommand("select   photo from   Tb where  ID=1",conn);     
  SqlDataReader   reader=cmd.ExecuteReader();     
  reader.Read();     
  MemoryStream   buf=new   MemoryStream((byte[])reader[0]);     
  Image   image=Image.FromStream(buf,true);     
  pictureBox1.Image=image; 
}    
  
using(System.IO.FileStream stream = new System.IO.FileStreamfile,System.IO.FileMode.Open,System.IO.FileAccess.Read){
    byte[] buffer = new byte[stream.Length]; 
    stream.Read(buffer, 0, (int)stream.Length); 
    stream.Close(); 
    string strName = System.IO.Path.GetFileNameWithoutExtension(file); 
    SqlCommand cmd = new SqlCommand("", sqlConn); 
    cmd.Parameters.Add("@photo", SqlDbType.Image).Value = buffer; 
    cmd.ExecuteNonQuery();}

解决方案 »

  1.   

            /// <summary>
            /// 上传图片文件
            /// </summary>
            /// <param name="fid"></param>
            public void upImg(int fid)
            {
                FileDialog fdimg;
                fdimg = new System.Windows.Forms.OpenFileDialog();
                fdimg.Filter = "JPG 文件|*.jpg";
                if (fdimg.ShowDialog() == DialogResult.OK)
                {
                    string filename = fdimg.FileName;
                    FileInfo fi = new FileInfo(filename);
                    byte[] imgStream = new byte[fi.Length];
                    FileStream fs=new FileStream(filename,FileMode.Open,FileAccess.Read);
                    fs.Read(imgStream, 0,imgStream.Length);
                    Fidiograph = imgStream;
                    Fuserid = fid;
                    Update();
                }
            }
            public void Update()
            {
                StringBuilder strSql = new StringBuilder();
                strSql.Append("update dbo.table set ");
                strSql.Append("Fidiograph=@Fidiograph");
                strSql.Append(" where FID=@FID ");
                SqlParameter[] parameters = {
    new SqlParameter("@ID", SqlDbType.Int,4),
    new SqlParameter("@Fidiograph", SqlDbType.Image)};
                parameters[0].Value = _Fid;
                parameters[1].Value = _Fidiograph;
    //
    sql语句执行部分,执行上传
    //
            }
      

  2.   

    using(SqlConnection conn=new SqlConnection(""))

    conn.Open(); 
    SqlCommand cmd=new SqlCommand("select photo from Tb where ID=1",conn); 
    SqlDataReader reader=cmd.ExecuteReader(); 
    reader.Read(); 
    MemoryStream buf=new MemoryStream((byte[])reader[0]); 
    Image image=Image.FromStream(buf,true); 
    pictureBox1.Image=image; 
    } using(System.IO.FileStream stream = new System.IO.FileStreamfile,System.IO.FileMode.Open,System.IO.FileAccess.Read){
    byte[] buffer = new byte[stream.Length]; 
    stream.Read(buffer, 0, (int)stream.Length); 
    stream.Close(); 
    string strName = System.IO.Path.GetFileNameWithoutExtension(file); 
    SqlCommand cmd = new SqlCommand("", sqlConn); 
    cmd.Parameters.Add("@photo", SqlDbType.Image).Value = buffer; 
    cmd.ExecuteNonQuery();}