In this tutorial I show you how update delete data from mySQL database using C# and Visual Studio. In Previous tutorials I show you how to connect with database, how insert data and select data. In here include codes and query regarding this tutorial.
Database Connection Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;
namespace TestProject
{
class DBConnect
{
public static MySqlConnection ConnectDB()
{
try
{
string server = "localhost";
string database = "testproject";
string uid = "root";
string password = "";
string connectionString;
connectionString = "SERVER=" + server + ";" + "DATABASE=" + database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";
MySqlConnection connection = new MySqlConnection(connectionString);
return connection;
}
catch (MySqlException ex)
{
// MessageBox.Show(ex.Message);
return null;
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;
namespace TestProject
{
class DBConnect
{
public static MySqlConnection ConnectDB()
{
try
{
string server = "localhost";
string database = "testproject";
string uid = "root";
string password = "";
string connectionString;
connectionString = "SERVER=" + server + ";" + "DATABASE=" + database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";
MySqlConnection connection = new MySqlConnection(connectionString);
return connection;
}
catch (MySqlException ex)
{
// MessageBox.Show(ex.Message);
return null;
}
}
}
C# form with query and 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 TestProject
{
public partial class Form1 : Form
{
MySqlConnection con = null;
public Form1()
{
InitializeComponent();
con = DBConnect.ConnectDB();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void btnadd_Click(object sender, EventArgs e)
{
con.Open();
try
{
string qu = "INSERT INTO `test`(`id`, `name`) VALUES ('"+txtid.Text+"', '"+txtnames.Text+"')";
MySqlCommand cm = new MySqlCommand(qu, con);
cm.ExecuteNonQuery();
MessageBox.Show("Data insert");
}
catch (Exception ex)
{
}
con.Close();
select();
}
private void btnSelect_Click(object sender, EventArgs e)
{
select();
}
private void select()
{
string sql = "SELECT `id`, `name` FROM `test`";
con.Open();
MySqlCommand sCommand = new MySqlCommand(sql, con);
MySqlDataAdapter sAdapter = new MySqlDataAdapter(sCommand);
DataTable dt = new DataTable();
sAdapter.Fill(dt);
dataTBL.DataSource = dt;
con.Close();
}
private void btnupdate_Click(object sender, EventArgs e)
{
try
{
con.Open();
string su = "UPDATE `test` SET `name`='"+txtnames.Text+"' WHERE id='"+txtid.Text+"'";
MySqlCommand cm = new MySqlCommand(su, con);
cm.ExecuteNonQuery();
con.Close();
select();
MessageBox.Show("Data update");
}
catch (Exception ex)
{
MessageBox.Show(ex + "");
}
}
private void btnDelete_Click(object sender, EventArgs e)
{
try
{
con.Open();
string su = "DELETE FROM `test` WHERE id='"+txtid.Text+"'";
MySqlCommand cm = new MySqlCommand(su, con);
cm.ExecuteNonQuery();
con.Close();
select();
MessageBox.Show("Successfully Delete");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
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 TestProject
{
public partial class Form1 : Form
{
MySqlConnection con = null;
public Form1()
{
InitializeComponent();
con = DBConnect.ConnectDB();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void btnadd_Click(object sender, EventArgs e)
{
con.Open();
try
{
string qu = "INSERT INTO `test`(`id`, `name`) VALUES ('"+txtid.Text+"', '"+txtnames.Text+"')";
MySqlCommand cm = new MySqlCommand(qu, con);
cm.ExecuteNonQuery();
MessageBox.Show("Data insert");
}
catch (Exception ex)
{
}
con.Close();
select();
}
private void btnSelect_Click(object sender, EventArgs e)
{
select();
}
private void select()
{
string sql = "SELECT `id`, `name` FROM `test`";
con.Open();
MySqlCommand sCommand = new MySqlCommand(sql, con);
MySqlDataAdapter sAdapter = new MySqlDataAdapter(sCommand);
DataTable dt = new DataTable();
sAdapter.Fill(dt);
dataTBL.DataSource = dt;
con.Close();
}
private void btnupdate_Click(object sender, EventArgs e)
{
try
{
con.Open();
string su = "UPDATE `test` SET `name`='"+txtnames.Text+"' WHERE id='"+txtid.Text+"'";
MySqlCommand cm = new MySqlCommand(su, con);
cm.ExecuteNonQuery();
con.Close();
select();
MessageBox.Show("Data update");
}
catch (Exception ex)
{
MessageBox.Show(ex + "");
}
}
private void btnDelete_Click(object sender, EventArgs e)
{
try
{
con.Open();
string su = "DELETE FROM `test` WHERE id='"+txtid.Text+"'";
MySqlCommand cm = new MySqlCommand(su, con);
cm.ExecuteNonQuery();
con.Close();
select();
MessageBox.Show("Successfully Delete");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
No comments:
Post a Comment