Kết nối C# với DataBase MySQL | C# with MySQL

Kết nối C# với DataBase MySQL | C# with MySQL

Bài viết hôm nay sẽ hướng dẫn các bạn kết nối C# với CSDL MySQL

Kết nối C# với DataBase MySQL | C# with MySQL
Giao dien WinForm


Source 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 KetNoiMySQL
{
    public partial class Form1 : Form
    {
        MySqlConnection Conn;
        MySqlCommandBuilder cmd;
        MySqlDataAdapter adap;
        DataTable mytable;
        string strconn = "Server = localhost; Database = danhsachsv; UId = root; Pwd = 123456; Pooling=false;Character Set=utf8";
        string query_select = "SELECT * FROM sinhvien";
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                Conn = new MySqlConnection(strconn);
                Conn.Open();
                adap = new MySqlDataAdapter(query_select, Conn);
                cmd = new MySqlCommandBuilder(adap);
                mytable = new DataTable();
                adap.Fill(mytable);
                Conn.Close();
                dataGridView1.DataSource = mytable;
            }
            catch (MySqlException)
            {
                MessageBox.Show("Lỗi kết nối MySQL", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void Btn_Add_Click(object sender, EventArgs e)
        {
            Conn.Open();
            string query_insert = "INSERT INTO sinhvien (`HoTen`, `NamSinh`, `DiaChi`) VALUES ('" + TB_HoTen.Text + "','" + TB_NamSinh.Text + "','" + TB_DiaChi.Text + "')";
            MySqlCommand command = new MySqlCommand(query_insert, Conn);
            command.ExecuteNonQuery();
            Conn.Close();
            Read_Data();
        }

        private void Read_Data()
        {
            Conn.Open();
            adap = new MySqlDataAdapter(query_select, Conn);
            cmd = new MySqlCommandBuilder(adap);
            mytable = new DataTable();
            adap.Fill(mytable);
            Conn.Close();
            dataGridView1.DataSource = mytable;
        }

        private void Btn_Edit_Click(object sender, EventArgs e)
        {
            Conn.Open();
            string query_edit = "UPDATE sinhvien SET `HoTen`='" + TB_HoTen.Text + "', `NamSinh`='" + TB_NamSinh.Text + "', `DiaChi`='" + TB_DiaChi.Text + "' WHERE Id = '" + int.Parse(TB_Id.Text) + "'";
            MySqlCommand command = new MySqlCommand(query_edit, Conn);
            command.ExecuteNonQuery();
            Conn.Close();
            Read_Data();
        }

        private void Btn_Delete_Click(object sender, EventArgs e)
        {
            Conn.Open();
            string query_delete = "DELETE FROM sinhvien WHERE `Id`='" + int.Parse(TB_Id.Text) + "'";
            MySqlCommand command = new MySqlCommand(query_delete, Conn);
            command.ExecuteNonQuery();
            Conn.Close();
            Read_Data();
        }
    }

}


Link tải file DLL:

Video hướng dẫn:


1 comment: