Programming for Everybody: June 2024

c# tutorial for beginners: How to store images update and delete in sql database with code





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.Data.SqlClient;

using System.IO;


namespace Images

{

    public partial class Form2 : Form

    {

        public Form2()

        {

            InitializeComponent();

        }

        SqlConnection conn = new SqlConnection("Data source=.;Initial catalog=images3;Integrated Security = true");

        SqlCommand cmd;

        private void button1_Click(object sender, EventArgs e)

        {

            openFileDialog1.Filter = "Select image(*.JpG; *.png; *.Gif)|*.JpG; *.png; *.Gif";

            if (openFileDialog1.ShowDialog()== DialogResult.OK)

            {

                pictureBox1.Image = Image.FromFile(openFileDialog1.FileName);

            }

        }


        private void button2_Click(object sender, EventArgs e)

        {

            cmd = new SqlCommand("Insert Into table1(name_image,image1)Values(@name_image,@image1)", conn);

            cmd.Parameters.AddWithValue("name_image", textBox1.Text);

            MemoryStream memstr = new MemoryStream();

            pictureBox1.Image.Save(memstr, pictureBox1.Image.RawFormat);

            cmd.Parameters.AddWithValue("image1", memstr.ToArray());

            conn.Open();

            cmd.ExecuteNonQuery();

            conn.Close();

            MessageBox.Show("Data Inserted Successfully");

            load_data();

        }

        private void load_data()

        {

            cmd = new SqlCommand("Select * from table1 order by id desc", conn);

            SqlDataAdapter da = new SqlDataAdapter();

            da.SelectCommand = cmd;

            DataTable dt = new DataTable();

            dt.Clear();

            da.Fill(dt);

            dataGridView1.RowTemplate.Height = 75;

            dataGridView1.DataSource = dt;

            DataGridViewImageColumn pic1 = new DataGridViewImageColumn();

            pic1 = (DataGridViewImageColumn)dataGridView1.Columns[2];

            pic1.ImageLayout = DataGridViewImageCellLayout.Stretch;

        }


        private void Form2_Load(object sender, EventArgs e)

        {

            load_data();

        }


        private void dataGridView1_Click(object sender, EventArgs e)

        {

            id1.Text = dataGridView1.CurrentRow.Cells[0].Value.ToString();

            textBox1.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();

            MemoryStream ms = new MemoryStream((byte[])dataGridView1.CurrentRow.Cells[2].Value);

            pictureBox1.Image = Image.FromStream(ms);

        }


        private void button3_Click(object sender, EventArgs e)

        {

            cmd = new SqlCommand("Update table1 Set name_image = @name_image,image1=@image1 Where id=@id", conn);

            cmd.Parameters.AddWithValue("name_image", textBox1.Text);

            MemoryStream memstr = new MemoryStream();

            pictureBox1.Image.Save(memstr, pictureBox1.Image.RawFormat);

            cmd.Parameters.AddWithValue("image1", memstr.ToArray());

            cmd.Parameters.AddWithValue("id", id1.Text);

            conn.Open();

            cmd.ExecuteNonQuery();

            conn.Close();

            load_data();

        }


        private void button4_Click(object sender, EventArgs e)

        {

            cmd = new SqlCommand("Delete from table1 where id=@id", conn);

            cmd.Parameters.AddWithValue("id", id1.Text);

            conn.Open();

            cmd.ExecuteNonQuery();

            conn.Close();

            load_data();

            pictureBox1.Image = null;

            textBox1.Text = "";

            id1.Text = "";

        }

    }

}