Programming for Everybody: Insert images to database
Showing posts with label Insert images to database. Show all posts
Showing posts with label Insert images to database. Show all posts

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 = "";

        }

    }

}


How to insert image into a SQL database using Visual Basic.net(with Source Code+database)











Imports System.Data.SqlClient
Imports System.IO
Imports System.Drawing.Imaging

Public Class Form2


    Dim con1 As New SqlConnection("Server=.;Database=images;integrated security=true")

    Private Sub Form2_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        bind1()
    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click


        Dim openfiledialog1 As New OpenFileDialog


        openfiledialog1.Filter = "images|*.jpg;*.png;*.gif;*.bmp"


        If openfiledialog1.ShowDialog = Windows.Forms.DialogResult.OK Then



            PictureBox1.Image = Image.FromFile(openfiledialog1.FileName)


        End If

    End Sub

    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click


        Dim command1 As New SqlCommand("Insert into table1(name,age,img1)values(@name,@age,@img)", con1)

        command1.Parameters.Add("@name", SqlDbType.VarChar).Value = TextBox1.Text


        command1.Parameters.Add("@age", SqlDbType.VarChar).Value = TextBox2.Text



        Dim memstr As New MemoryStream


        PictureBox1.Image.Save(memstr, PictureBox1.Image.RawFormat)

        command1.Parameters.Add("@img", SqlDbType.Image).Value = memstr.ToArray


        If con1.State = ConnectionState.Closed Then

            con1.Open()


        End If

        command1.ExecuteNonQuery()

        MessageBox.Show("image inserted")

        con1.Close()

        bind1()

    End Sub


    Private Sub bind1()

        Dim command1 As New SqlCommand("Select * from table1 order by id desc", con1)


        Dim adapter As New SqlDataAdapter(command1)


        Dim table As New DataTable()

        adapter.Fill(table)

        DataGridView1.AllowUserToAddRows = False

        DataGridView1.RowTemplate.Height = 90


        Dim pic1 As New DataGridViewImageColumn

        DataGridView1.DataSource = table

        pic1 = DataGridView1.Columns(3)

        pic1.ImageLayout = DataGridViewImageCellLayout.Stretch


    End Sub



    Private Sub DataGridView1_Click(sender As Object, e As System.EventArgs) Handles DataGridView1.Click


        Label4.Text = DataGridView1.CurrentRow.Cells(0).Value

        TextBox1.Text = DataGridView1.CurrentRow.Cells(1).Value


        TextBox2.Text = DataGridView1.CurrentRow.Cells(2).Value



        Dim img As Byte()

        img = DataGridView1.CurrentRow.Cells(3).Value

        Dim memstr As New MemoryStream(img)


        PictureBox1.Image = Image.FromStream(memstr)




    End Sub

    Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click


        Dim memstr As New MemoryStream

        PictureBox1.Image.Save(memstr, PictureBox1.Image.RawFormat)


        Dim img() As Byte

        img = memstr.ToArray()

        Dim update1 As String = "Update table1 set name='" & TextBox1.Text & "',age='" & TextBox2.Text & "',img1=@img where id=" & Label4.Text

        Dim command1 As New SqlCommand(update1, con1)

        command1.Parameters.Add("@img", SqlDbType.Image).Value = img

        If con1.State = ConnectionState.Closed Then

            con1.Open()
        End If

        command1.ExecuteNonQuery()

        MessageBox.Show("Image updated")

        con1.Close()

        bind1()

    End Sub

    Private Sub Button4_Click(sender As System.Object, e As System.EventArgs) Handles Button4.Click


        Dim delete1 As String = "Delete from table1 where id=" & Label4.Text

        Dim command1 As New SqlCommand(delete1, con1)

        If con1.State = ConnectionState.Closed Then

            con1.Open()

        End If

        command1.ExecuteNonQuery()

        MessageBox.Show("Image deleted")


        con1.Close()


        bind1()


    End Sub
End Class