C# And MySQL - How To Insert Update Delete Data In MySQL Database Using C#

C# - How To Insert Update Delete Data In MySQL Database Using C#

__________________________________________________________________________

In This C# Tutorial We Will See How To Add Update Delete Data In MySQL Database In CSharp Programming Language .


                                        1 - How To Insert Tutorial 

 1 - How To UpdateTutorial 


 1 - How To Delete Tutorial 

 1 - How To Insert Update Delete Tutorial 


11 - How To Insert 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 CSharp_MySQL_Insert : Form
    {
        MySqlConnection connection = new MySqlConnection("datasource=localhost;port=3306;username=root;password=");
        public CSharp_MySQL_Insert()
        {
            InitializeComponent();
        }

        private void BTN_INSERT_Click(object sender, EventArgs e)
        {
            string insertQuery = "INSERT INTO test_db.users(fname,lname,age) VALUES('" + textBoxFName.Text + "','" + textBoxLName.Text + "'," + textBoxAge.Text + ")";
            connection.Open();
            MySqlCommand command = new MySqlCommand(insertQuery, connection);

            try
            {
                if (command.ExecuteNonQuery() == 1)
                {
                    MessageBox.Show("Data Inserted");
                }
                else
                {
                    MessageBox.Show("Data Not Inserted");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            connection.Close();

        }
    }
}

11 - How To Update 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 Csharp_MySQL_Update : Form
    {

        MySqlConnection connection = new MySqlConnection("datasource=localhost;port=3306;username=root;password=");

        public Csharp_MySQL_Update()
        {
            InitializeComponent();
        }

        private void BTN_UPDATE_Click(object sender, EventArgs e)
        {
          
            string updateQuery = "UPDATE test_db.users SET fname = '"+textBoxFName.Text+"', lname = '"+textBoxLName.Text+"', age = "+int.Parse(textBoxAge.Text)+" WHERE id="+int.Parse(textBoxID.Text);

            connection.Open();
            try
            {
                MySqlCommand command = new MySqlCommand(updateQuery, connection);
                if (command.ExecuteNonQuery() == 1)
                {
                    MessageBox.Show("DATA UPDATED");
                }
                else
                {
                    MessageBox.Show("Data NOT UPDATED");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            connection.Close();
        }
    }
}

11 - How To Delete 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 Csharp_MySQL_Delete : Form
    {

        MySqlConnection connection = new MySqlConnection("datasource=localhost;port=3306;username=root;password=");
        public Csharp_MySQL_Delete()
        {
            InitializeComponent();
        }

        private void BTN_DELETE_Click(object sender, EventArgs e)
        {
            
            try
            {
                string deleteQuery = "DELETE FROM test_db.users WHERE id = " + textBoxID.Text;
                connection.Open();
                MySqlCommand command = new MySqlCommand(deleteQuery, connection);

                if (command.ExecuteNonQuery() == 1)
                {
                    MessageBox.Show("USER DELETED");
                }
                else
                {
                    MessageBox.Show("USER NOT DELETED");
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            connection.Close();

        }
    }
}

11 - How To Insert Update Delete 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 Csharp_MySQL_Insert_Update_Delete : Form
    {
        MySqlConnection connection = new MySqlConnection("datasource=localhost;port=3306;username=root;password=");
        MySqlCommand command;
        public Csharp_MySQL_Insert_Update_Delete()
        {
            InitializeComponent();
        }

        public void openConnection()
        {
            if(connection.State == ConnectionState.Closed)
            {
                connection.Open();
            }
        }

        public void closeConnection()
        {
            if(connection.State == ConnectionState.Open)
            {
                connection.Close();
            }
        }

        public void executeQuery( String query)
        {
            try
            {
                openConnection();
                command = new MySqlCommand(query, connection);
                if (command.ExecuteNonQuery() == 1)
                {
                    MessageBox.Show("Query Executed");
                }else
                {
                    MessageBox.Show("Query Not Executed");
                }
            }
            catch(Exception ex){
                MessageBox.Show(ex.Message);
            }finally{
                closeConnection();
            }
        }

        private void BTN_INSERT_Click(object sender, EventArgs e)
        {
            string insertQuery = "INSERT INTO test_db.users(fname,lname,age) VALUES('"+textBoxFName.Text+"','"+textBoxLName.Text+"',"+textBoxAge.Text+")";
            executeQuery(insertQuery);
        }

        private void BTN_UPDATE_Click(object sender, EventArgs e)
        {
            string updateQuery = "UPDATE test_db.users SET fname='" + textBoxFName.Text + "', lname='" + textBoxLName.Text + "', age=" + textBoxAge.Text + " WHERE id = " + textBoxID.Text;
            executeQuery(updateQuery);

        }

        private void BTN_DELETE_Click(object sender, EventArgs e)
        {
            string deleteQuery = "DELETE FROM test_db.users WHERE id = "+textBoxID.Text;
            executeQuery(deleteQuery);
        }
    }
}


=> OUTPUTS:

c# database insert

c# database update

c# databse delete

c# database insert update delete





Share this

Related Posts

Previous
Next Post »

2 comments

comments