Insert Records into MySQL database in C#

Insert Records into MySQL database in C#

Embeddings into the database in C#: Here, we will figure out how to embed records into MySQL database utilizing C#.net?

In the last instructional exercise (how to interface with MySQL database in C#?), we found out about making the association with MySQL database in C#. Here, in this instructional exercise, we will figure out how to embed the records in MySQL database in C#?

Here, we are making a Windows Application, that will store understudy records into the database. So as a matter of first importance, we will choose the database utilizing “show databases” and afterwards change/utilize the database “use mysqltest“, here “use mysqltest” is the database name.

Insert Records into MySQL database in C#
Insert Records into MySQL database in C#

Presently, we will make an understudy table utilizing a “make table” order, in which we will embed the records.

rollno It is utilized to store the move number of understudy.
name It is utilized to store the name of understudy.
age It is utilized to store the period of understudy.
class It is utilized to store a class of understudy in school.
fee It is utilized to store expense sum

In the above table, we made after sections,

Presently we will build up an application to store understudy data into the database.

C# application to store understudies data into MySQL database

Insert Records into MySQL database in C#

In the above application, we changed after properties,

    Form
    Name	:	frmStuInfo
    Text	:	Student Information

    Label1
    Text	:	Roll Number

    Label2
    Text	:	Name

    Label3
    Text	:	Age

    Label4
    Text	:	Class

    Label5
    Text	:	Fee

    Button1
    Text 	:	Save To Database

C# code to embed records into MySQL Database

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using MySql.Data.MySqlClient;

namespace Insertion
{
    public partial class frmStuInfo : Form
    {
        public frmStuInfo()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MySqlConnection conn = new MySqlConnection("server=localhost;database=mysqltest;uid=root;pwd=root");

            MySqlCommand cmd = null;
            string cmdString = "";
            conn.Open();

            cmdString = "insert into student values(" + textBox1.Text + ",'" + textBox2.Text + "'," + textBox3.Text + ",'" + textBox4.Text + "'," + textBox5.Text + ")";

            cmd = new MySqlCommand(cmdString, conn);
            cmd.ExecuteNonQuery();

            conn.Close();

            MessageBox.Show("Data Stored Successfully");
        }
    }
}

Presently In this above source code, we incorporate a namespace for MySQL database availability is: “MySql.Data.MySqlClient”.

Furthermore, we utilized two classes MySqlConnection and MySqlCommand and here we get ready association string to interface with the database.

"server=localhost;database=mysqltest;uid=root;pwd=root" 

Furthermore, we arranged an additional order with embedded textbox values. Furthermore, execute the order utilizing the ExecuteNonQuery() strategy for MySqlCommand class. Lastly, close association and show fitting message utilizing MessageBox.

Presently look to the output of use,

Insert Records into MySQL database in C#

In the above application, we filled data with respect to an understudy and tapped on the order button the given data will be put away into the database. Presently we will register data with the MYSQL database utilizing MySQL brief.

Insert Records into MySQL database in C#

Here, we can see our data given by application is appropriately spared into the database.

Recent Posts

error: Alert: Content is protected!!