C# CRUD MYSQL SERVER XAMPP
SCHOOL (SEKOLAH ) PROGRAM SAMPLE
C# WITH MYSQL SERVER XAMPP
Bismillah
Pada tahap awal ini untuk membuat aplikasi C# with MySql Server Xampp kita membutuhkan beberapa toll diantara nya adalah :
- MYSQL SERVER XAMPP Link Download
Mysql Server Xampp menggunakan versi Bintami xampp-windows-x64-8.2.12-0-VS16-installer
Tampilan Xampp Control Panel V.3.3.0 saat di Running start dijalan :
Dalam Pembuatan databese Mysql Server Xampp pada kali menggunakan Google Chrome dan boleh dengan Browser lain dengan mengetikkan :http://localhost/phpmyadmin/ seperti gambar berikut:
Dalam pembuatan Database kali ini dengan nama data_siswa klik tombol buat seperti gambar berikut:
Tabel - tabel yang digunakan dalam database data_siswa antara lain :- TABLE `guru`
- TABLE `mapel`
- TABLE `nilai`
- TABLE `siswa`
- TABLE `users`
- TABLE `vmapel`
- TABLE `vnilai`
Link Download Database
Dengan Sql Query sebagai berikut : data_siswa.sql
- TABLE `guru`
--------------------
CREATE TABLE `guru` (
`id` int(4) NOT NULL,
`nama` varchar(100) NOT NULL,
`nip` varchar(25) NOT NULL,
`created_at` timestamp NULL DEFAULT current_timestamp(),
`updated_at` timestamp NULL DEFAULT NULL ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
---------------------------------------------------------------------------------------------------------
- TABLE `mapel`
---------------------
CREATE TABLE `mapel` (
`id` int(4) NOT NULL,
`nama_mapel` varchar(50) NOT NULL,
`kode_mapel` varchar(10) NOT NULL,
`guru_id` int(4) NOT NULL,
`created_at` timestamp NULL DEFAULT current_timestamp(),
`updated_at` timestamp NULL DEFAULT NULL ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
---------------------------------------------------------------------------------------------------------
- TABLE `nilai`
-------------------
CREATE TABLE `nilai` (
`id` int(6) NOT NULL,
`siswa_id` int(4) NOT NULL,
`mapel_id` int(3) NOT NULL,
`nilai` int(11) NOT NULL DEFAULT 0,
`created_at` timestamp NULL DEFAULT current_timestamp(),
`updated_at` timestamp NULL DEFAULT NULL ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_swedish_ci;
--------------------------------------------------------------------------------------------------------
- TABLE `siswa
---------------------
CREATE TABLE `siswa` (
`id` int(4) NOT NULL,
`nama` varchar(150) NOT NULL,
`jenkel` varchar(10) NOT NULL,
`alamat` varchar(255) NOT NULL,
`nohp` varchar(18) NOT NULL,
`jurusan` varchar(15) NOT NULL,
`email` varchar(20) NOT NULL,
`created_at` timestamp NULL DEFAULT current_timestamp(),
`updated_at` timestamp NULL DEFAULT NULL ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_swedish_ci;
----------------------------------------------------------------------------------------------------
- TABLE `users`
-----------------------
CREATE TABLE `users` (
`id` int(4) NOT NULL,
`name` varchar(150) NOT NULL,
`email` varchar(25) NOT NULL,
`email_verified_at` timestamp NULL DEFAULT NULL,
`password` varchar(255) NOT NULL,
`level` varchar(15) NOT NULL,
`created_at` timestamp NULL DEFAULT current_timestamp(),
`updated_at` timestamp NULL DEFAULT NULL ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_swedish_ci;
----------------------------------------------------------------------------------------------------
- TABLE `vmapel`
-----------------------
CREATE TABLE `vmapel` (
`id` int(4)
,`nama_mapel` varchar(50)
,`kode_mapel` varchar(10)
,`nama` varchar(100)
);
----------------------------------
- TABLE `vnilai`
--------------------
CREATE TABLE `vnilai` (
`id` int(6)
,`nama` varchar(150)
,`jurusan` varchar(15)
,`nama_mapel` varchar(50)
,`guru` varchar(100)
,`nilai` int(11)
);
--------------------------------
Berikut tampilan Design Form pada SCHOOL (SEKOLAH ) PROGRAM SAMPLE yang kita gunakan dari Form Login,Form Menu dan Form lainnya :
Form Login
Source code program sebagai berikut :
Form Login
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 sekolah
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public MySqlCommand cmd { get; private set; }
public MySqlDataReader dr { get; private set; }
private void btlogin_Click(object sender, EventArgs e)
{
Koneksi.conn.Open();
String query = "SELECT * FROM `users` WHERE `email` = '" + txtusername.Text + "' AND `password` = '" + txtpassword.Text + "'";
cmd = new MySqlCommand(query, Koneksi.conn);
dr = cmd.ExecuteReader();
try
{
if (dr.Read())
{
new Dashboard().Show();
this.Hide();
}
else
{
MessageBox.Show("Ups! Username atau Password anda Salah");
}
}
catch (Exception)
{
MessageBox.Show("Ups! Gagal Login");
}
Koneksi.conn.Close();
}
}
}
----------------------------------------------------------------------------------------------------------------------------
Form Dashboard
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;
namespace sekolah
{
public partial class Dashboard : Form
{
public Dashboard()
{
InitializeComponent();
}
private void btsiswa_Click(object sender, EventArgs e)
{
if (ActiveMdiChild != null)
ActiveMdiChild.Close();
panel3.Visible = false;
Siswa FormAktif = new Siswa();
FormAktif.MdiParent = this;
FormAktif.Show();
label1.Text = "Kelola Data Siswa";
}
private void btguru_Click(object sender, EventArgs e)
{
if (ActiveMdiChild != null)
ActiveMdiChild.Close();
panel3.Visible = false;
Guru FormAktif = new Guru();
FormAktif.MdiParent = this;
FormAktif.Show();
label1.Text = "Kelola Data Guru";
}
private void btmapel_Click(object sender, EventArgs e)
{
if (ActiveMdiChild != null)
ActiveMdiChild.Close();
panel3.Visible = false;
Mapel FormAktif = new Mapel();
FormAktif.MdiParent = this;
FormAktif.Show();
label1.Text = "Kelola Data Mata Pelajaran";
}
private void btnilai_Click(object sender, EventArgs e)
{
if (ActiveMdiChild != null)
ActiveMdiChild.Close();
panel3.Visible = false;
Nilai FormAktif = new Nilai();
FormAktif.MdiParent = this;
FormAktif.Show();
label1.Text = "Kelola Data Nilai";
}
private void panel4_Paint(object sender, PaintEventArgs e)
{
if (ActiveMdiChild != null)
ActiveMdiChild.Close();
panel3.Visible = true;
label1.Text = "Aplikasi Pendataan Nilai";
}
private void btlogout_Click(object sender, EventArgs e)
{
new Form1().Show();
this.Hide();
}
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------------
Form Siswa
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;
using Mysqlx.Crud;
namespace sekolah
{
public partial class Siswa : Form
{
public MySqlCommand cmd { get; private set; }
public string id { get; private set; }
private void Tampil()
{
try
{
//MessageBox.Show("Ini Muncul Saat Form dipanggil");
Koneksi.conn.Open(); //ini membuka koneksi database
//ini digunakan untuk mengambil data di tabel siswa
MySqlDataAdapter da = new MySqlDataAdapter("SELECT * FROM `data_siswa`.`siswa`", Koneksi.conn);
//membuat objek Dasa Set Baru
DataSet ds = new DataSet();
//ini digunakan untuk mengisi dataset dari Data Adapter
da.Fill(ds);
//Mengisi DataGrid Siswa dengan DataSet
dgsiswa.DataSource = ds.Tables[0];
Koneksi.conn.Close();
}
catch (Exception)
{
MessageBox.Show("Duh!!, Ada Error Nih");
}
}
public Siswa()
{
InitializeComponent();
}
private void Siswa_Load(object sender, EventArgs e)
{
Tampil();
btDelete.Enabled = false;
btUpdate.Enabled = false;
btCancel.Enabled = false;
}
private void Clear()
{
txtnama.Text = "";
txtalamat.Text = "";
txtemail.Text = "";
txtjurusan.Text = "";
txtnohp.Text = "";
txtcari.Text = "";
btDelete.Enabled = false;
btUpdate.Enabled = false;
btCancel.Enabled = false;
btInsert.Enabled = true;
}
private void btInsert_Click(object sender, EventArgs e)
{
try
{
Koneksi.conn.Open();
String Queri = "INSERT INTO siswa (`nama`, `jenkel`, `alamat`, `nohp`, `jurusan`, `email`) VALUES ('" + txtnama.Text + "', '" + cbkelamin.Text + "', '" + txtalamat.Text + "', '" + txtnohp.Text + "', '" + txtjurusan.Text + "', '" + txtemail.Text + "')";
cmd = new MySqlCommand(Queri, Koneksi.conn);
cmd.ExecuteNonQuery();
MessageBox.Show("Berhasil Simpan Data Siswa");
Koneksi.conn.Close();
Tampil(); //memanggil method Tampil
Clear(); //memanggil method Clear
}
catch (Exception)
{
MessageBox.Show("Tambah Data Gagal");
}
}
private void dgsiswa_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
btDelete.Enabled = true;
btUpdate.Enabled = true;
btCancel.Enabled = true;
btInsert.Enabled = false;
int baris = dgsiswa.CurrentCell.RowIndex;
id = dgsiswa.Rows[baris].Cells[0].Value.ToString();
txtnama.Text = dgsiswa.Rows[baris].Cells[1].Value.ToString();
cbkelamin.Text = dgsiswa.Rows[baris].Cells[2].Value.ToString();
txtalamat.Text = dgsiswa.Rows[baris].Cells[3].Value.ToString();
txtnohp.Text = dgsiswa.Rows[baris].Cells[4].Value.ToString();
txtjurusan.Text = dgsiswa.Rows[baris].Cells[5].Value.ToString();
txtemail.Text = dgsiswa.Rows[baris].Cells[6].Value.ToString();
}
private void btUpdate_Click(object sender, EventArgs e)
{
try
{
Koneksi.conn.Open();
String Queri = "UPDATE siswa SET `nama`='" + txtnama.Text + "', `jenkel`='" + cbkelamin.Text + "', `alamat`='" + txtalamat.Text + "', ` nohp`='" + txtnohp.Text + "', `jurusan`='" + txtjurusan.Text + "', `email`='" + txtemail.Text + "' WHERE `id`='" + id + "'";
cmd = new MySqlCommand(Queri, Koneksi.conn);
cmd.ExecuteNonQuery();
MessageBox.Show("Berhasil Update Data Siswa");
Koneksi.conn.Close();
Tampil();
Clear();
}
catch (Exception)
{
MessageBox.Show("Update Gagal");
}
}
private void btDelete_Click(object sender, EventArgs e)
{
try
{
Koneksi.conn.Open();
cmd = new MySqlCommand("DELETE FROM siswa WHERE `id`= '" + id + "'", Koneksi.conn);
cmd.ExecuteNonQuery();
MessageBox.Show("Berhasil Hapus Data Siswa");
Koneksi.conn.Close();
Tampil();
Clear();
}
catch (Exception)
{
MessageBox.Show("Hapus Data Gagal");
}
}
private void btCancel_Click(object sender, EventArgs e)
{
Clear();
}
private void txtcari_TextChanged(object sender, EventArgs e)
{
try
{
Koneksi.conn.Open();
MySqlDataAdapter da = new MySqlDataAdapter("SELECT * FROM `siswa` where nama LIKE '%" + txtcari.Text + "%'", Koneksi.conn);
DataSet ds = new DataSet();
da.Fill(ds);
dgsiswa.DataSource = ds.Tables[0];
Koneksi.conn.Close();
}
catch (Exception)
{
MessageBox.Show("Duh!!, Ada Error Nih");
}
}
}
}
-----------------------------------------------------------------------------------------------------------------------------
Form Guru
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 sekolah
{
public partial class Guru : Form
{
public MySqlCommand cmd { get; private set; }
public string id { get; private set; }
private void Tampil()
{
try
{
Koneksi.conn.Open();
MySqlDataAdapter da = new MySqlDataAdapter("SELECT * FROM `guru`", Koneksi.conn);
DataSet ds = new DataSet();
da.Fill(ds);
dgguru.DataSource = ds.Tables[0];
Koneksi.conn.Close();
}
catch (Exception)
{
MessageBox.Show("Duh!!, Ada Error Nih");
}
}
public Guru()
{
InitializeComponent();
}
private void Guru_Load(object sender, EventArgs e)
{
Tampil();
btDelete.Enabled = false;
btUpdate.Enabled = false;
btCancel.Enabled = false;
}
private void Clear()
{
txtnama.Text = "";
txtnip.Text = "";
btDelete.Enabled = false;
btUpdate.Enabled = false;
btCancel.Enabled = false;
btInsert.Enabled = true;
}
private void btInsert_Click(object sender, EventArgs e)
{
try
{
Koneksi.conn.Open();
String Queri = "INSERT INTO guru (`nama`, `nip`) VALUES ('" + txtnama.Text + "', '" + txtnip.Text + "')";
cmd = new MySqlCommand(Queri, Koneksi.conn);
cmd.ExecuteNonQuery();
MessageBox.Show("Berhasil Simpan Data Guru");
Koneksi.conn.Close();
Tampil();
Clear();
}
catch (Exception)
{
MessageBox.Show("Tambah Data Gagal");
}
}
private void dgguru_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
btDelete.Enabled = true;
btUpdate.Enabled = true;
btCancel.Enabled = true;
btInsert.Enabled = false;
int baris = dgguru.CurrentCell.RowIndex;
id = dgguru.Rows[baris].Cells[0].Value.ToString();
txtnama.Text = dgguru.Rows[baris].Cells[1].Value.ToString();
txtnip.Text = dgguru.Rows[baris].Cells[2].Value.ToString();
}
private void btUpdate_Click(object sender, EventArgs e)
{
try
{
Koneksi.conn.Open();
String Queri = "UPDATE guru SET `nama`='" + txtnama.Text + "', `nip`='" + txtnip.Text + "' WHERE `id`='" + id + "'";
cmd = new MySqlCommand(Queri, Koneksi.conn);
cmd.ExecuteNonQuery();
MessageBox.Show("Berhasil Update Data Guru");
Koneksi.conn.Close();
Tampil();
Clear();
}
catch (Exception)
{
MessageBox.Show("Update Gagal");
}
}
private void btDelete_Click(object sender, EventArgs e)
{
try
{
Koneksi.conn.Open();
cmd = new MySqlCommand("DELETE FROM guru WHERE `id`= '" + id + "'", Koneksi.conn);
cmd.ExecuteNonQuery();
MessageBox.Show("Berhasil Hapus Data Guru");
Koneksi.conn.Close();
Tampil();
Clear();
}
catch (Exception)
{
MessageBox.Show("Hapus Data Gagal");
}
}
private void btCancel_Click(object sender, EventArgs e)
{
Clear();
}
private void txtcari_TextChanged(object sender, EventArgs e)
{
try
{
Koneksi.conn.Open();
MySqlDataAdapter da = new MySqlDataAdapter("SELECT * FROM `guru` where nama LIKE '%" + txtcari.Text + "%'", Koneksi.conn);
DataSet ds = new DataSet();
da.Fill(ds);
dgguru.DataSource = ds.Tables[0];
Koneksi.conn.Close();
}
catch (Exception)
{
MessageBox.Show("Duh!!, Ada Error Nih");
}
}
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------------
Form Mapel
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 sekolah
{
public partial class Mapel : Form
{
public MySqlCommand cmd { get; private set; }
public string id { get; private set; }
private void Tampil()
{
try
{
Koneksi.conn.Open();
MySqlDataAdapter da = new MySqlDataAdapter("SELECT * FROM `vmapel`", Koneksi.conn);
DataSet ds = new DataSet();
da.Fill(ds);
dgmapel.DataSource = ds.Tables[0];
Koneksi.conn.Close();
}
catch (Exception)
{
MessageBox.Show("Duh!!, Ada Error Nih");
}
}
public Mapel()
{
InitializeComponent();
}
private void Mapel_Load(object sender, EventArgs e)
{
Tampil();
MySqlDataAdapter adp = new MySqlDataAdapter("select * from guru", Koneksi.conn);
DataTable dt = new DataTable();
adp.Fill(dt);
cbnamaguru.DataSource = dt;
cbnamaguru.DisplayMember = "nama";
cbnamaguru.ValueMember = "id";
}
private void Clear()
{
txtnamamapel.Text = "";
txtkodemapel.Text = "";
txtcari.Text = "";
cbnamaguru.Text = "";
btDelete.Enabled = false;
btUpdate.Enabled = false;
btCancel.Enabled = false;
btInsert.Enabled = true;
}
private void btInsert_Click(object sender, EventArgs e)
{
try
{
Koneksi.conn.Open();
String Queri = "INSERT INTO mapel (`nama_mapel`, `kode_mapel`, `guru_id`) VALUES ('" + txtnamamapel.Text + "', '" + txtkodemapel.Text + "', '" + cbnamaguru.SelectedValue + "')";
cmd = new MySqlCommand(Queri, Koneksi.conn);
cmd.ExecuteNonQuery();
MessageBox.Show("Berhasil Simpan Data Mapel");
Koneksi.conn.Close();
Tampil();
Clear();
}
catch (Exception)
{
MessageBox.Show("Tambah Data Gagal");
}
}
private void dgmapel_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
btDelete.Enabled = true;
btUpdate.Enabled = true;
btCancel.Enabled = true;
btInsert.Enabled = false;
int baris = dgmapel.CurrentCell.RowIndex;
id = dgmapel.Rows[baris].Cells[0].Value.ToString();
txtnamamapel.Text = dgmapel.Rows[baris].Cells[1].Value.ToString();
txtkodemapel.Text = dgmapel.Rows[baris].Cells[2].Value.ToString();
cbnamaguru.Text = dgmapel.Rows[baris].Cells[3].Value.ToString();
}
private void btUpdate_Click(object sender, EventArgs e)
{
try
{
Koneksi.conn.Open();
String Queri = "UPDATE mapel SET `nama_mapel`='" + txtnamamapel.Text + "', `kode_mapel`='" + txtkodemapel.Text + "', `guru_id`='" + cbnamaguru.SelectedValue + "' WHERE `id`='" + id + "'";
cmd = new MySqlCommand(Queri, Koneksi.conn);
cmd.ExecuteNonQuery();
MessageBox.Show("Berhasil Update Data Guru");
Koneksi.conn.Close();
Tampil();
Clear();
}
catch (Exception)
{
MessageBox.Show("Update Gagal");
}
}
private void btDelete_Click(object sender, EventArgs e)
{
try
{
Koneksi.conn.Open();
cmd = new MySqlCommand("DELETE FROM mapel WHERE `id`= '" + id + "'", Koneksi.conn);
cmd.ExecuteNonQuery();
MessageBox.Show("Berhasil Hapus Data mapel");
Koneksi.conn.Close();
Tampil();
Clear();
}
catch (Exception)
{
MessageBox.Show("Hapus Data Gagal");
}
}
private void btCancel_Click(object sender, EventArgs e)
{
Clear();
}
private void txtcari_TextChanged(object sender, EventArgs e)
{
try
{
Koneksi.conn.Open();
MySqlDataAdapter da = new MySqlDataAdapter("SELECT * FROM `vmapel` where nama_mapel LIKE '%" + txtcari.Text + "%'", Koneksi.conn);
DataSet ds = new DataSet();
da.Fill(ds);
dgmapel.DataSource = ds.Tables[0];
Koneksi.conn.Close();
}
catch (Exception)
{
MessageBox.Show("Duh!!, Ada Error Nih");
}
}
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------------
Form Nilai
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 sekolah
{
public partial class Nilai : Form
{
public MySqlCommand cmd { get; private set; }
public string id { get; private set; }
private void Tampil()
{
try
{
Koneksi.conn.Open();
MySqlDataAdapter da = new MySqlDataAdapter("SELECT * FROM `vnilai`", Koneksi.conn);
DataSet ds = new DataSet();
da.Fill(ds);
dgnilai.DataSource = ds.Tables[0];
Koneksi.conn.Close();
}
catch (Exception)
{
MessageBox.Show("Duh!!, Ada Error Nih");
}
}
public Nilai()
{
InitializeComponent();
}
private void Nilai_Load(object sender, EventArgs e)
{
Tampil();
MySqlDataAdapter adp = new MySqlDataAdapter("select * from mapel", Koneksi.conn);
DataTable dt = new DataTable();
adp.Fill(dt);
cbmapel.DataSource = dt;
cbmapel.DisplayMember = "nama_mapel";
cbmapel.ValueMember = "id";
MySqlDataAdapter adp2 = new MySqlDataAdapter("select * from siswa", Koneksi.conn);
DataTable dt2 = new DataTable();
adp2.Fill(dt2);
cbsiswa.DataSource = dt2;
cbsiswa.DisplayMember = "nama";
cbsiswa.ValueMember = "id";
btDelete.Enabled = false;
btUpdate.Enabled = false;
btCancel.Enabled = false;
}
private void Clear()
{
cbmapel.Text = "";
cbsiswa.Text = "";
txtnilai.Text = "";
txtcari.Text = ""
btDelete.Enabled = false;
btUpdate.Enabled = false;
btCancel.Enabled = false;
btInsert.Enabled = true;
}
private void btInsert_Click(object sender, EventArgs e)
{
try
{
Koneksi.conn.Open();
String Queri = "INSERT INTO nilai (`siswa_id`, `mapel_id`, `nilai`) VALUES ('" + cbsiswa.SelectedValue + "','" + cbmapel.SelectedValue + "', '" + txtnilai.Text + "')";
cmd = new MySqlCommand(Queri, Koneksi.conn);
cmd.ExecuteNonQuery();
MessageBox.Show("Berhasil Simpan Nilai Siswa");
Koneksi.conn.Close();
Tampil();
Clear();
}
catch (Exception)
{
MessageBox.Show("Tambah Data Gagal");
}
}
private void dgnilai_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
btDelete.Enabled = true;
btUpdate.Enabled = true;
btCancel.Enabled = true;
btInsert.Enabled = false;
int baris = dgnilai.CurrentCell.RowIndex;
id = dgnilai.Rows[baris].Cells[0].Value.ToString();
cbsiswa.Text = dgnilai.Rows[baris].Cells[1].Value.ToString();
cbmapel.Text = dgnilai.Rows[baris].Cells[3].Value.ToString();
txtnilai.Text = dgnilai.Rows[baris].Cells[5].Value.ToString();
}
private void btUpdate_Click(object sender, EventArgs e)
{
try
{
Koneksi.conn.Open();
String Queri = "UPDATE `nilai` SET `siswa_id`='" + cbsiswa.SelectedValue + "', `mapel_id`='" + cbmapel.SelectedValue + "', `nilai`='" + txtnilai.Text + "' WHERE `id`='" + id + "'";
cmd = new MySqlCommand(Queri, Koneksi.conn);
cmd.ExecuteNonQuery();
MessageBox.Show("Berhasil Update Data Siswa");
Koneksi.conn.Close();
Tampil();
Clear();
}
catch (Exception)
{
MessageBox.Show("Update Gagal");
}
}
private void btDelete_Click(object sender, EventArgs e)
{
try
{
Koneksi.conn.Open();
cmd = new MySqlCommand("DELETE FROM nilai WHERE `id`= '" + id + "'", Koneksi.conn);
cmd.ExecuteNonQuery();
MessageBox.Show("Berhasil Hapus Data Siswa");
Koneksi.conn.Close();
Tampil();
Clear();
}
catch (Exception)
{
MessageBox.Show("Hapus Data Gagal");
}
}
private void btCancel_Click(object sender, EventArgs e)
{
try
{
Koneksi.conn.Open();
MySqlDataAdapter da = new MySqlDataAdapter("SELECT * FROM `vnilai` where nama LIKE '%" + txtcari.Text + "%'", Koneksi.conn);
DataSet ds = new DataSet();
da.Fill(ds);
dgnilai.DataSource = ds.Tables[0];
Koneksi.conn.Close();
}
catch (Exception)
{
MessageBox.Show("Duh!!, Ada Error Nih");
}
}
private void txtcari_TextChanged(object sender, EventArgs e)
{
try
{
Koneksi.conn.Open();
MySqlDataAdapter da = new MySqlDataAdapter("SELECT * FROM `vnilai` where nama LIKE '%" + txtcari.Text + "%'", Koneksi.conn);
DataSet ds = new DataSet();
da.Fill(ds);
dgnilai.DataSource = ds.Tables[0];
Koneksi.conn.Close();
}
catch (Exception)
{
MessageBox.Show("Duh!!, Ada Error Nih");
}
}
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------------
Modul Class : Koneksi.cs
-------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;
namespace sekolah
{
internal class Koneksi
{
public static MySqlConnection conn = new MySqlConnection("Server=127.0.0.1;Port=3306;UID=root;PWD=;Database=data_siswa");
}
}
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Untuk koneksi ke Mysql Server Xampp Program SCHOOL (SEKOLAH ) PROGRAM SAMPLE menggunakan MySql.Data dalam koneksi koneksi database seperti gambar berikut :
Dalam Visual Studio 2022 klik menu + pilih klik Nuget Package Manager + pilih Klik Manage Nugets Packages for Solutions maka akan tampil sebagai berikut :
Selanjutnya pilih Browse pada pencarian Search (Ctrl + L) ketikkan Mysql seperti tampilan berikut :
Lalu akan tampil MySql.Data by MySql, 12M download [MySql.Data.MySqlClient.Net Core Class Library] dan pilih klik dan akan tampil di sebelahnya dikanan Menu MySql Data lalu klik cek list project dan sekolah lalu klik Install.
Alhamdulillah
Selamat Mencoba
Komentar
Posting Komentar