In this tutorial I show you how display data in table from mySQL database using C# and Visual Studio. last tutorial I make database connection here I load data to grid view using that database connection. In here include all source code regarding these tutorials.
Reference file here >>
C# form 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