C# Insert All DataGridView Data In MySQL

How To Add All DataGridView Values Into MySQL Database Using C#

nsert All DataGridView Records In MySQL Database Using C#,

In This C# Tutorial  We Will See How To Populate A Datagridview From Datatable And Add All Datagridview Row's Records In MySQL Database Using For Loop And Mysqlcommand with Parameters In Csharp Programming Language And Visual Studio Editor.


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_Tutorials
{
    public partial class Insert_All_DGV_Data_Into_MySQL : Form
    {
        public Insert_All_DGV_Data_Into_MySQL()
        {
            InitializeComponent();
        }

        // mysql connection
        MySqlConnection connection = new MySqlConnection("datasource=localhost;port=3306;Initial Catalog='mydb';username=root;password=");
        
        private void Insert_All_DGV_Data_Into_MySQL_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;

        }

        // button insert
        private void buttonInsertAllData_Click(object sender, EventArgs e)
        {

            MySqlCommand command;

            connection.Open();

            for (int i = 0; i < dataGridView1.Rows.Count - 1 ; i++ )
            {
                command = new MySqlCommand("INSERT INTO dgv_data(`first_name`, `last_name`, `age`) VALUES(@ID,@fn,@ln,@AGE)", connection);

                command.Parameters.Add("@ID", MySqlDbType.Int32).Value = dataGridView1.Rows[i].Cells[0].Value.ToString();
                command.Parameters.Add("@fn", MySqlDbType.VarChar).Value = dataGridView1.Rows[i].Cells[1].Value.ToString();
                command.Parameters.Add("@ln", MySqlDbType.VarChar).Value = dataGridView1.Rows[i].Cells[2].Value.ToString();
                command.Parameters.Add("@AGE", MySqlDbType.Int32).Value = dataGridView1.Rows[i].Cells[3].Value.ToString();

                command.ExecuteNonQuery();
             }

            connection.Close();

        }
    }
}

      

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





Share this

Related Posts

Previous
Next Post »