public string minechiefCertPaper = System.Configuration.ConfigurationManager.ConnectionStrings["MineChiefCertPaper"].ConnectionString.ToString(); private void button1_Click(object sender, EventArgs e) { //insert photo into the database string fullPath = this.textBox3.Text; FileStream fs = new FileStream(fullPath, FileMode.Open); byte[] imageBytes = new byte[fs.Length]; BinaryReader reader = new BinaryReader(fs); imageBytes = reader.ReadBytes(Convert.ToInt32(fs.Length)); SqlConnection conn = new SqlConnection(this.minechiefCertPaper); conn.Open(); // update coalchiefneed set zhaopian='33' where tablename='t_ap_busicoalchiefcertify' and fid=1 SqlCommand cmd = new SqlCommand("update " + this.textBox1.Text + " set zhaopian=@zhaopian where tablename='t_ap_busicoalchiefcertify' and fid=" + this.textBox2.Text, conn); SqlParameter param1 = new SqlParameter(); param1.ParameterName = "zhaopian"; param1.SqlDbType = SqlDbType.Image; param1.Value = imageBytes; cmd.Parameters.Add(param1); cmd.ExecuteNonQuery(); conn.Close(); } private void textBox3_Click(object sender, EventArgs e) { openFileDialog1.Filter = "jpg图片|*.JPG|GIF图片|*.gif|BMP图片|*.bmp|所有文件|*.*"; if (this.openFileDialog1.ShowDialog() == DialogResult.OK) { this.textBox3.Text = this.openFileDialog1.FileName; } } #region 将数据库中的图片读出来的代码片断 //try{ //SqlConnection conn=new SqlConnection('IntegratedSecurity=SSPI;Persist Security Info=False;Initial Catalog=DatabaseImage;DataSource=(local)'); //conn.Open(); //SqlCommand comm=conn.CreateCommand(); //comm.CommandText='select* from Images where id=@id'; //comm.CommandType=CommandType.Text; //comm.Parameters.Add('@id', SqlDbType.BigInt).Value=int.Parse(Request['id']); //SqlDataReader reader=comm.ExecuteReader(); //while(reader.Read()) //{ //Response.ContentType=reader['contentType'].ToString(); //Response.BinaryWrite((byte[])reader['Image']); //} //Response.End(); //conn.Close(); //} //catch //{ //Response.End(); //} #endregion //将图片读出来 private void button2_Click(object sender, EventArgs e) { pictureBox1.Image = null; SqlConnection conn = new SqlConnection(this.minechiefCertPaper); string strSql = "SELECT zhaopian from coalchiefneed where tablename='t_ap_busiCoalChiefCertify' and fid="+this.textBox2.Text; SqlCommand cmd = new SqlCommand(strSql, conn); conn.Open(); SqlDataReader reader = cmd.ExecuteReader(); reader.Read(); //创建一个内存 读取数据流,将读取的数据库的图片以二进制的byte[]流存入内存中。 MemoryStream ms = new MemoryStream((byte[])reader["zhaopian"]); reader.Close(); conn.Close(); pictureBox1.Image = Image.FromStream(ms, true); //从刚才存储到内存的数据流中创建Image对象。 //picturebox的缩放模式为StretchImage }