日期:2014-05-20 浏览次数:20968 次
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;