C# - Insert, Update, Delete, Search Image In MySQL Database

How To Insert Update Delete Search Display Images In MySQL Database Using C#

c# and mysql add, edit, remove, find, show images


In This C# Tutorial We Will See How To :

- Add Image Into MySQL Database
- Edit Selected Image From MySQL Database
- Delete Selected Image From MySQL Database
- Search Image By ID From MySQL Database
- Show Image From MySQL Database Into Datagridview

Using CSharp Programming Language.


                     =>   Part 2

                      =>  Part 3

                      =>  Part 4

                       => Part 5

                       => Part 6

                       => Part 7

                       => Part 8

Project Source 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 MySql.Data.MySqlClient;
using System.IO;
using System.Drawing.Imaging;

namespace Csharp_And_MySQL
{
    public partial class Display_Insert_Update_Delete_Search_Image_In_MySQL_Database : Form
    {
        public Display_Insert_Update_Delete_Search_Image_In_MySQL_Database()
        {
            InitializeComponent();
        }
        MySqlConnection connection = new MySqlConnection("datasource=localhost;port=3306;Initial Catalog='db_images';username=root;password=");
       
        private void Display_Insert_Update_Delete_Search_Image_In_MySQL_Database_Load(object sender, EventArgs e)
        {
            FillDGV("");
        }

        public void FillDGV( string valueToSearch)
        {

            MySqlCommand command = new MySqlCommand("SELECT * FROM myimages WHERE CONCAT(ID, Name, Description) LIKE '%" + valueToSearch + "%'", connection);

            MySqlDataAdapter adapter = new MySqlDataAdapter(command);

            DataTable table = new DataTable();

            adapter.Fill(table);

            dataGridView1.RowTemplate.Height = 60;

            dataGridView1.AllowUserToAddRows = false;

            dataGridView1.DataSource = table;

            DataGridViewImageColumn imgCol = new DataGridViewImageColumn();
            imgCol = (DataGridViewImageColumn)dataGridView1.Columns[3];
            imgCol.ImageLayout = DataGridViewImageCellLayout.Stretch;

            dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;

        }

        private void BTN_CHOOSE_IMAGE_Click(object sender, EventArgs e)
        {

            OpenFileDialog opf = new OpenFileDialog();

            opf.Filter = "Choose Image(*.JPG;*.PNG;*.GIF)|*.jpg;*.png;*.gif";

            if(opf.ShowDialog() == DialogResult.OK)
            {
                pictureBox1.Image = Image.FromFile(opf.FileName);
            }

        }

        private void dataGridView1_Click(object sender, EventArgs e)
        {

            Byte[] img = (Byte[])dataGridView1.CurrentRow.Cells[3].Value;

            MemoryStream ms = new MemoryStream(img);

            pictureBox1.Image = Image.FromStream(ms);

            textBoxID.Text   = dataGridView1.CurrentRow.Cells[0].Value.ToString();
            textBoxName.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
            textBoxDesc.Text = dataGridView1.CurrentRow.Cells[2].Value.ToString();

        }

        private void BTN_INSERT_Click(object sender, EventArgs e)
        {

            MemoryStream ms = new MemoryStream();
            pictureBox1.Image.Save(ms, pictureBox1.Image.RawFormat);
            byte[] img = ms.ToArray();

            MySqlCommand command = new MySqlCommand("INSERT INTO myimages(ID, Name, Description, Image) VALUES (@id,@name,@desc,@img)", connection);

            command.Parameters.Add("@id", MySqlDbType.VarChar).Value = textBoxID.Text;
            command.Parameters.Add("@name", MySqlDbType.VarChar).Value = textBoxName.Text;
            command.Parameters.Add("@desc", MySqlDbType.VarChar).Value = textBoxDesc.Text;
            command.Parameters.Add("@img", MySqlDbType.Blob).Value = img;

            ExecMyQuery(command, "Data Inserted");

        }

        public void ExecMyQuery(MySqlCommand mcomd, string myMsg)
        {
            connection.Open();
            if(mcomd.ExecuteNonQuery() == 1)
            {

                MessageBox.Show(myMsg);

            }else{

                MessageBox.Show("Query Not Executed");

            }

            connection.Close();

            FillDGV("");
        }

        private void BTN_UPDATE_Click(object sender, EventArgs e)
        {

            MemoryStream ms = new MemoryStream();
            pictureBox1.Image.Save(ms, pictureBox1.Image.RawFormat);
            byte[] img = ms.ToArray();

            MySqlCommand command = new MySqlCommand("UPDATE myimages SET Name=@name, Description=@desc, Image=@img WHERE ID = @id", connection);

            command.Parameters.Add("@id", MySqlDbType.VarChar).Value = textBoxID.Text;
            command.Parameters.Add("@name", MySqlDbType.VarChar).Value = textBoxName.Text;
            command.Parameters.Add("@desc", MySqlDbType.VarChar).Value = textBoxDesc.Text;
            command.Parameters.Add("@img", MySqlDbType.Blob).Value = img;

            ExecMyQuery(command, "Data Updated");

        }

        private void BTN_DELETE_Click(object sender, EventArgs e)
        {

            MySqlCommand command = new MySqlCommand("DELETE FROM myimages WHERE ID = @id", connection);

            command.Parameters.Add("@id", MySqlDbType.VarChar).Value = textBoxID.Text;

            ExecMyQuery(command, "Data Deleted");

            ClearFields();

        }

        private void textBoxSearch_TextChanged(object sender, EventArgs e)
        {
            FillDGV(textBoxSearch.Text);
        }

        private void BTN_FIND_Click(object sender, EventArgs e)
        {
            MySqlCommand command = new MySqlCommand("SELECT * FROM myimages WHERE ID = @id", connection);
            command.Parameters.Add("@id", MySqlDbType.VarChar).Value = textBoxID.Text;

            MySqlDataAdapter adapter = new MySqlDataAdapter(command);

            DataTable table = new DataTable();

            adapter.Fill(table);

            if(table.Rows.Count <= 0)
            {
                MessageBox.Show("No Data Found");
                ClearFields();
            }
            else
            {

                textBoxID.Text = table.Rows[0][0].ToString();
                textBoxName.Text = table.Rows[0][1].ToString();
                textBoxDesc.Text = table.Rows[0][2].ToString();

                byte[] img = (byte[])table.Rows[0][3];
                MemoryStream ms = new MemoryStream(img);
                pictureBox1.Image = Image.FromStream(ms);

            }
        }

        private void BTN_NEW_Click(object sender, EventArgs e)
        {
            ClearFields();
        }

        public void ClearFields()
        {
            textBoxID.Text = "";
            textBoxName.Text = "";
            textBoxDesc.Text = "";

            pictureBox1.Image = null;

        }

    }
}


///////////////OUTPUT:


show, find, add, edit, remove, images from mysql database using c#




VB.Net - Insert, Update, Delete, Search Image In SQL

How To Insert Update Delete Search Display Images In SQL Database Using VB.Net

vb.net insert update delete search display image in database


In This VB.NET Tutorial We Will See How To :

- Add Image Into SQL Server Database
- Edit Selected Image From SQL Server Database
- Delete Selected Image From SQL Server Database
- Search Image By ID From SQL Server Database
- Show Image From SQL Server Database Into Datagridview

Using Visual Basic .NET Programming Language.


                     =>   Part 2

                      =>  Part 3

                      =>  Part 4

                       => Part 5

                       => Part 6

                       => Part 7


Project Source Code:

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

Public Class VbNet_Insert_Update_Delete_Search_Display_Images_From_SQL

    Dim connection As New SqlConnection("Server= SAMSNG-PC; Database = TestDB; Integrated Security = true")
    Private Sub VbNet_Insert_Update_Delete_Search_Display_Images_From_SQL_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        Dim command As New SqlCommand("select * from table_images", connection)
        Dim adapter As New SqlDataAdapter(command)
        Dim table As New DataTable()
        adapter.Fill(table)

        DataGridView1.AllowUserToAddRows = False

        DataGridView1.RowTemplate.Height = 100
        Dim imgc As New DataGridViewImageColumn
        DataGridView1.DataSource = table

        imgc = DataGridView1.Columns(3)
        imgc.ImageLayout = DataGridViewImageCellLayout.Stretch


    End Sub

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

        Dim opf As New OpenFileDialog

        opf.Filter = "Choose Image(*.JPG;*.PNG;*.GIF)|*.jpg;*.png;*.gif"

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

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

        End If

        Dim ms As New MemoryStream
        PictureBox1.Image.Save(ms, PictureBox1.Image.RawFormat)

    End Sub

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

        Dim img As Byte()
        img = DataGridView1.CurrentRow.Cells(3).Value
        Dim ms As New MemoryStream(img)
        PictureBox1.Image = Image.FromStream(ms)

        TextBoxID.Text = DataGridView1.CurrentRow.Cells(0).Value
        TextBoxName.Text = DataGridView1.CurrentRow.Cells(1).Value
        TextBoxDesc.Text = DataGridView1.CurrentRow.Cells(2).Value

    End Sub

    Public Sub ExecuteMyQuery(MyCommand As SqlCommand, MyMessage As String)

        connection.Open()

        If MyCommand.ExecuteNonQuery = 1 Then

            MessageBox.Show(MyMessage)

        Else

            MessageBox.Show("Query Not Executed")

        End If

        connection.Close()

        populateDatagridview("")

    End Sub

    Private Sub BTN_INSERT_Click(sender As Object, e As EventArgs) Handles BTN_INSERT.Click

        Dim ms As New MemoryStream
        PictureBox1.Image.Save(ms, PictureBox1.Image.RawFormat)
        Dim img() As Byte
        img = ms.ToArray()
        Dim insertQuery As String = "INSERT INTO Table_Images(name,description,the_image) VALUES('" & TextBoxName.Text & "','" & TextBoxDesc.Text & "', @img )"

        Dim command As New SqlCommand(insertQuery, connection)
        command.Parameters.Add("@img", SqlDbType.Image).Value = img

        ExecuteMyQuery(command, " IMage Inserted ")

    End Sub

    Private Sub BTN_UPDATE_Click(sender As Object, e As EventArgs) Handles BTN_UPDATE.Click

        Dim ms As New MemoryStream
        PictureBox1.Image.Save(ms, PictureBox1.Image.RawFormat)
        Dim img() As Byte
        img = ms.ToArray()
        Dim updateQuery As String = "UPDATE Table_Images SET name = '" & TextBoxName.Text & "',description = '" & TextBoxDesc.Text & "',the_image = @img WHERE id = " & TextBoxID.Text

        Dim command As New SqlCommand(updateQuery, connection)
        command.Parameters.Add("@img", SqlDbType.Image).Value = img

        ExecuteMyQuery(command, " IMage Updated ")

    End Sub

    Private Sub BTN_DELETE_Click(sender As Object, e As EventArgs) Handles BTN_DELETE.Click

        Dim deleteQuery As String = "DELETE FROM Table_Images WHERE id = " & TextBoxID.Text

        Dim command As New SqlCommand(deleteQuery, connection)

        ExecuteMyQuery(command, " IMage Deleted ")

    End Sub

    Public Sub populateDatagridview(valueToSearch As String)

        Dim searchQuery As String = "SELECT * From Table_Images WHERE CONCAT(name,description) like '%" & valueToSearch & "%'"

        Dim command As New SqlCommand(searchQuery, connection)
        Dim adapter As New SqlDataAdapter(command)
        Dim table As New DataTable()
        adapter.Fill(table)

        DataGridView1.AllowUserToAddRows = False

        DataGridView1.RowTemplate.Height = 100
        Dim imgc As New DataGridViewImageColumn
        DataGridView1.DataSource = table

        imgc = DataGridView1.Columns(3)
        imgc.ImageLayout = DataGridViewImageCellLayout.Stretch

    End Sub

    Private Sub TextBoxSearch_TextChanged(sender As Object, e As EventArgs) Handles TextBoxSearch.TextChanged

        populateDatagridview(TextBoxSearch.Text)

    End Sub

    Private Sub BTN_FIND_Click(sender As Object, e As EventArgs) Handles BTN_FIND.Click

        Dim command As New SqlCommand("select * from table_images WHERE id = @id ", connection)

        command.Parameters.Add("@id", SqlDbType.Int).Value = TextBoxID.Text

        Dim adapter As New SqlDataAdapter(command)

        Dim table As New DataTable()

        adapter.Fill(table)

        If table.Rows.Count() <= 0 Then

            MessageBox.Show("No Data Found")

        Else

            TextBoxID.Text = table.Rows(0)(0).ToString()
            TextBoxName.Text = table.Rows(0)(1).ToString()
            TextBoxDesc.Text = table.Rows(0)(2).ToString()

            Dim img() As Byte

            img = table.Rows(0)(3)

            Dim ms As New MemoryStream(img)

            PictureBox1.Image = Image.FromStream(ms)

        End If

    End Sub
End Class

///////////////OUTPUT:


vb.net insert update delete search display image from sql server database




C# - Show Images From Folder On Datagridview Row Click

How To Display Image From Folder On DataGridView Click Event Using C#

c# datagridview display image


In This C# Tutorial  We Will See How To Show Selected Image Name From DataGridView In Another Form Using CSharp Programming Language.


Project Source 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.IO;

namespace WindowsFormsApplication1
{
    public partial class Show_Images_From_Folder_1 : Form
    {
        public Show_Images_From_Folder_1()
        {
            InitializeComponent();
        }

        private void Show_Images_From_Folder_1_Load(object sender, EventArgs e)
        {
            string[] files = Directory.GetFiles(@"D:\Images");
            DataTable table = new DataTable();
            table.Columns.Add("File Name");

            for(int i = 0; i < files.Length; i++)
            {
                FileInfo file = new FileInfo(files[i]);
                table.Rows.Add(file.Name);
            }

            dataGridView1.DataSource = table;

        }

        private void dataGridView1_DoubleClick(object sender, EventArgs e)
        {
            Show_Images_From_Folder_2 myForm = new Show_Images_From_Folder_2();
            string imageName = dataGridView1.CurrentRow.Cells[0].Value.ToString();
            Image img;
            img = Image.FromFile(@"D:\Images\"+imageName);
            myForm.pictureBox1.Image = img;
            myForm.ShowDialog();
        }
    }
}

      
///////////////OUTPUT:

datagridview show image on click event





C# - Populate Datagridview With Files Name's

How To Fill DataGridView With Files Name From Folder Values On Another Form Using C#

                                                                                                                         

In This C# Tutorial  We Will See How To Bind DataGridView File Name's Form Folder Using CSharp Programming Language.


Project Source 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.IO;

namespace WindowsFormsApplication1
{
    public partial class DataGridView_With_Files_Name : Form
    {
        public DataGridView_With_Files_Name()
        {
            InitializeComponent();
        }

        private void DataGridView_With_Files_Name_Load(object sender, EventArgs e)
        {

            String[] files = Directory.GetFiles(@"C:\Users\samsng\Desktop\products");
            DataTable table = new DataTable();
            table.Columns.Add("File Name");

            for(int i = 0; i < files.Length; i++)
            {
                FileInfo file = new FileInfo(files[i]);
                table.Rows.Add(file.Name);
            }

            dataGridView1.DataSource = table; 

        }
    }
}

      
///////////////OUTPUT:





c# fill datagridview with file name's

C# - Export DataGridView To RichTextBox

How To Display DataGridView Data To RichTextBox Using C#

show datagridview values in richtextbox using c#


In This C# Tutorial  We Will See How To Show DataGridView Values In RichTextBox Using CSharp Programming Language.


Project Source 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;

namespace WindowsFormsApplication1
{
    public partial class Datagridview_To_RichTextBox : Form
    {
        public Datagridview_To_RichTextBox()
        {
            InitializeComponent();
        }

        private void Datagridview_To_RichTextBox_Load(object sender, EventArgs e)
        {
            DataTable table = new DataTable();

            // add columns to datatable
            table.Columns.Add("Id", typeof(int));
            table.Columns.Add("First Name", typeof(string));
            table.Columns.Add("Last Name", typeof(string));
            table.Columns.Add("Age", typeof(int));

            // add rows to datatable
            table.Rows.Add(1, "First A", "Last A", 10);
            table.Rows.Add(2, "First B", "Last B", 20);
            table.Rows.Add(3, "First C", "Last C", 30);
            table.Rows.Add(4, "First D", "Last D", 40);
            table.Rows.Add(5, "First E", "Last E", 50);
            table.Rows.Add(6, "First F", "Last F", 60);
            table.Rows.Add(7, "First G", "Last G", 70);
            table.Rows.Add(8, "First H", "Last H", 80);

            dataGridView1.DataSource = table;
        }

        private void button1_Click(object sender, EventArgs e)
        {

            for(int i = 0; i < dataGridView1.Rows.Count - 1; i++)
            {

                richTextBox1.Text = richTextBox1.Text + "-";

                for(int j = 0; j < dataGridView1.Columns.Count; j++)
                {

                    richTextBox1.Text = richTextBox1.Text + "\t" + dataGridView1.Rows[i].Cells[j].Value.ToString() + "\t";

                }

                richTextBox1.Text = richTextBox1.Text + "\n";
                richTextBox1.Text = richTextBox1.Text + "----------------------------------------------------------------------------------------------------------";
                richTextBox1.Text = richTextBox1.Text + "\n";
            }

        }
    }
}

      
///////////////OUTPUT:





datagridview To richtextbox

C# - Using MySQL Stored Procedure

call stored procedure in c#

How To Use MySQL Stored Procedure In C#

                                                                                                                         

In This C# Tutorial  We Will See How To Call MySQL Stored Procedure Using CSharp Programming Language.


Project Source 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 MySql.Data.MySqlClient;

namespace Csharp_And_MySQL
{
    public partial class Using_MySQL_Stored_Procedures : Form
    {
        public Using_MySQL_Stored_Procedures()
        {
            InitializeComponent();
        }

        MySqlConnection connection = new MySqlConnection("datasource=localhost;port=3306;Initial Catalog='test_db';username=root;password=");
        
        private void BTN_ADD_Click(object sender, EventArgs e)
        {
            MySqlParameter[] pms = new MySqlParameter[3];
            pms[0] = new MySqlParameter("fn", MySqlDbType.VarChar);
            pms[0].Value = textBoxFName.Text;

            pms[1] = new MySqlParameter("ln", MySqlDbType.VarChar);
            pms[1].Value = textBoxLName.Text;

            pms[2] = new MySqlParameter("age", MySqlDbType.Int32);
            pms[2].Value = textBoxAge.Text;

            MySqlCommand command = new MySqlCommand();

            command.Connection = connection;
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "SP_ADD_USER";

            command.Parameters.AddRange(pms);

            connection.Open();
            if(command.ExecuteNonQuery() == 1)
            {
                MessageBox.Show("Yes");
            }
            else
            {
                MessageBox.Show("No");
            }
            connection.Close();

        }
    }
}

      
///////////////OUTPUT:

using mysql stored procedure in c#