Home » C#

Insert Records into MySQL database in C#

Inserting into database in C#: Here, we are going to learn how to insert records into MySQL database using C#.net?
Submitted by IncludeHelp, on September 26, 2019

In the last tutorial (how to connect with MySQL database in C#?), we learned about making the connection with MySQL database in C#. Here, in this tutorial, we will learn how to insert the records in MySQL database in C#?

Here, we are creating a Windows Application, that will store student records into the database. So first of all, we will select the database using "show databases" and then change/use the database "use mysqltest", here "use mysqltest" is the database name.

Insert Records into MySQL database in C# | 1

Insert Records into MySQL database in C# | 2

Now, we will create a student table using "create table" command, in which we will insert the records.

Insert Records into MySQL database in C# | 3

In above table we created following columns,

rollno It is used to store roll number of student.
name It is used to store name of student.
age It is used to store age of student.
class It is used to store class of student in school.
fee It is used to store fee amount

Now we will develop an application to store student information into database.

C# application to store students information into MySQL database

Insert Records into MySQL database in C# | 4

In the above application we changed following 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 insert 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");
        }
    }
}

Now In this above source code, we include a namespace for MySQL database connectivity is: "MySql.Data.MySqlClient".

And we used two classes MySqlConnection and MySqlCommand and here we prepare connection string to connect with database.

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

And we prepared insert command with inserted textbox values. And execute the command using the ExecuteNonQuery() method of MySqlCommand class. And finally, close connection and show appropriate message using MessageBox.

Now look to the output of application,

Insert Records into MySQL database in C# | 5

In the above application, we filled information regarding a student and clicked on the command button the given information will be stored into the database. Now we will check information into the MYSQL database using MySQL prompt.

Insert Records into MySQL database in C# | 6

Here, we can see our information given by application is properly saved into database.



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.