Inserting records into Mysql database in .NET

Inserting records into Mysql database in .NET 

  • >create a form in .NET

 

  • >Open MySQL workbench > goto schemas in Navigator > go to above menubar > create a new schema in connection server and apply > right click on the database name you have created > create a table and give columns and apply 

 

  • >Now open .Net > open solution explorer > right click on References > Add Reference > Browse > open drive C: > open folder Program Files(x86) > open folder Mysql > open folder Connector NET > open folder Assemblies  >click on file Mysql.Data.dll 

 

  • >Now write code in insert data button 

private void button2_Click(object sender, EventArgs e)
        {
            MySqlConnection con = new MySqlConnection("server=localhost;database=netemploy;uid=root;pwd=root;");
            con.Open();
            MySqlCommand cmd= con.CreateCommand() ;
            //MessageBox.Show("connection seccesful");
            try
            {
                cmd.CommandText = "insert into employe_info(id,name,designation)VALUES(@id,@name,@desi)";
                cmd.Parameters.AddWithValue("@id", int.Parse(textBox1.Text));
                cmd.Parameters.AddWithValue("@name", textBox2.Text);
                cmd.Parameters.AddWithValue("@desi", textBox3.Text);
                cmd.ExecuteNonQuery();
                MessageBox.Show("seccesfuly inserted one record ");
            }
            catch (Exception) { throw; }
            finally
            {
                if (con.State == ConnectionState.Open)
                {
                    con.Close(); 
                }
            }
  
}


the green button is for inserting a record

code inside insert button

after execution insertion of record

 
above record inserted into the database

Comments