标签:
private void button1_Click(object sender, EventArgs e) { OpenFileDialog op = new OpenFileDialog(); if (op.ShowDialog()==DialogResult.OK) { FileStream fs = new FileStream(op.FileName,FileMode.Open); Image img = System.Drawing.Bitmap.FromStream(fs); pictureBox1.Image = img; fs.Close(); } } private void button2_Click(object sender, EventArgs e) { OpenFileDialog op = new OpenFileDialog(); if (op.ShowDialog() == DialogResult.OK) { FileStream fs = new FileStream(op.FileName, FileMode.Open); BinaryReader dr = new BinaryReader(fs); byte[] by=new byte[fs.Length]; by= dr.ReadBytes((int)fs.Length); SqlConnection conn = new SqlConnection("server=.;database=students_information;user=sa;pwd=123"); conn.Open(); SqlCommand cmd = conn.CreateCommand(); cmd.CommandText = "insert into img(img) values(@img)"; cmd.Parameters.Add(new SqlParameter("@img",by)); cmd.ExecuteNonQuery(); cmd.Dispose(); conn.Close(); MessageBox.Show("添加成功"); } } private void button3_Click(object sender, EventArgs e) { byte[] by = null; SqlConnection conn = new SqlConnection("server=.;database=students_information;user=sa;pwd=123"); conn.Open(); SqlCommand cmd = conn.CreateCommand(); cmd.CommandText = "select * from img"; SqlDataReader dr = cmd.ExecuteReader(); if (dr.Read()) { by = (byte[])dr["img"]; } conn.Close(); MemoryStream ms = new MemoryStream(by); Image img = System.Drawing.Bitmap.FromStream(ms); pictureBox1.Image = img; ms.Close(); }
标签:
原文地址:http://www.cnblogs.com/lushixiong/p/4561159.html