Hi Take 2 buttons, 2 picture boxes. One button is for inserting image into database another for retrieving that image from database to form.
Create table(photo) in SQL server with columns as donor_id,photo.
Code under button1_click:
private void button1_Click(object sender, EventArgs e) {
SqlConnection con = new SqlConnection("trusted_connection=yes;database=databasename;server=servername"); SqlCommand cmd = new SqlCommand("insert into photo(donor_id,photo) values(1,@Pic)", con);
MemoryStream stream = new MemoryStream(); pictureBox1.Image.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg); byte[] Pic = stream.ToArray(); cmd.Parameters.AddWithValue("@Pic", Pic); try { con.Open(); cmd.ExecuteNonQuery(); MessageBox.Show("success"); } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { con.Close(); } }
Code for button2_click:
private void button2_Click(object sender, EventArgs e) { SqlConnection connect = new SqlConnection("trusted_connection=yes;database=databasename;server=servername");
SqlCommand command = new SqlCommand("select (photo) from photo where donor_id='1'", connect);
SqlDataAdapter dp = new SqlDataAdapter(command); DataSet ds = new DataSet("MyImages");
byte[] MyData = new byte[0];
dp.Fill(ds, "MyImages"); DataRow myRow; myRow = ds.Tables["MyImages"].Rows[0];
MyData = (byte[])myRow["photo"];
MemoryStream stream1 = new MemoryStream(MyData); pictureBox2.Image = Image.FromStream(stream1); }
Attachmentscode for inserting & retriving image (23847-12529-Form1.txt)
|
No responses found. Be the first to respond and make money from revenue sharing program.
|