using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.IO; using System.Data.SqlClient; namespace main { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btnUpload_Click(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = "*.jpg|*.jpg|*.png|*.png|*.bmp|*.bmp"; if (ofd.ShowDialog() == DialogResult.OK) { string fileName = ofd.FileName; FileStream fs = new FileStream(fileName, FileMode.Open); byte[] imageBytes = new byte[fs.Length]; BinaryReader br = new BinaryReader(fs); imageBytes = br.ReadBytes(Convert.ToInt32(fs.Length)); string s = "server=PC-20171113RBMO;database=StudentDB;Trusted_Connection=true"; SqlConnection con = new SqlConnection(s); string c = "insert into Pic(image) values(@pic)"; SqlCommand cmd = new SqlCommand(c, con); SqlParameter para = new SqlParameter("@pic", SqlDbType.Image); para.Value = imageBytes; cmd.Parameters.Add(para); con.Open(); cmd.ExecuteNonQuery(); con.Close(); } } private void btnRead_Click(object sender, EventArgs e) { string s = "server=PC-20171113RBMO;database=StudentDB;Trusted_Connection=true"; SqlConnection con = new SqlConnection(s); string c = "select image from Pic where ID = " + textBox1.Text.Trim(); SqlCommand cmd = new SqlCommand(c, con); con.Open(); byte[] image = (byte[])cmd.ExecuteScalar(); con.Close(); MemoryStream ms = new MemoryStream(image); Bitmap bmp = new Bitmap(ms); pictureBox1.Image = bmp; } } }