Anda di halaman 1dari 6

aplikasi data mahasiswa

Data
using
using
using
using
using

System;
System.Collections.Generic;
System.Linq;
System.Text;
System.Threading.Tasks;

namespace FormCrud
{
class data
{
private string nim, nama, alamat, telpon;
public string Nim
{
get { return nim; }
set { nim = value; }
}
public string Nama
{
get { return nama; }
set { nama = value; }
}
public string Alamat
{
get { return alamat; }
set { alamat = value; }
}
public string Telpon
{
get { return telpon; }
set { telpon = value; }
}
}
}

Data Mahasiswa

using
using
using
using
using
using
using
using

MySql.Data.MySqlClient;
System;
System.Collections.Generic;
System.Data;
System.Data.SqlClient;
System.Linq;
System.Text;
System.Threading.Tasks;

namespace FormCrud
{
class Datamahasiswaamikom
{
private MySqlCommand perintah = null;
MySqlConnection koneksi = new MySqlConnection();
string koneksidatabase =
"Server=localhost;Port=3306;User=root;Password=;Database=belajar";
public Datamahasiswaamikom()
{
koneksi.ConnectionString = koneksidatabase;
}
public DataSet gedData()
{
DataSet dts = new DataSet();
try
{
koneksi.Open();
perintah = new MySqlCommand();
perintah.Connection = koneksi;
perintah.CommandType = CommandType.Text;
perintah.CommandText = "select Nim,Nama,Alamat,Telpon from tbldataamikom";
MySqlDataAdapter data = new MySqlDataAdapter(perintah);
data.Fill(dts, "tbldataamikom");
koneksi.Close();
}catch (MySqlException )
{}
return dts;
}
public bool Insert(data dt)
{
Boolean nilai = false;
try
{
koneksi.Open();
perintah = new MySqlCommand();
perintah.Connection = koneksi;
perintah.CommandType = CommandType.Text;
perintah.CommandText = "insert into tbldataamikom values('" + dt.Nim + "','" +
dt.Nama + "','" + dt.Alamat + "','" + dt.Telpon + "')";
perintah.ExecuteNonQuery();
nilai = true;
koneksi.Close();
}
catch (MySqlException) { }
return nilai;
}

public bool edit(data dt, string nim)


{
Boolean nilai = false;
try
{
koneksi.Open();
perintah = new MySqlCommand();
perintah.Connection = koneksi;
perintah.CommandType = CommandType.Text;
perintah.CommandText = "update tbldataamikom set Nim='" + dt.Nim +
"',Nama='" + dt.Nama + "',Alamat='" + dt.Alamat + "',Telpon='" + dt.Telpon + "' where
Nim='" + dt.Nim + "'";
perintah.ExecuteNonQuery();
nilai = true;
koneksi.Close();
}
catch (MySqlException) { }
return nilai;
}
public bool hapus(string nim)
{
Boolean nilai = false;
try
{
koneksi.Open();
perintah = new MySqlCommand();
perintah.Connection = koneksi;
perintah.CommandType = CommandType.Text;
perintah.CommandText = "Delete from tbldataamikom where Nim='" + nim + "'";
perintah.ExecuteNonQuery();
nilai = true;
koneksi.Close();
}
catch (MySqlException) { }
return nilai;
}
}
}

using System;
using System.Collections.Generic;
using System.Linq;

using System.Threading.Tasks;
using System.Windows.Forms;
namespace FormCrud
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}

Form

using
using
using
using
using
using
using
using
using
using

System;
System.Collections.Generic;
System.ComponentModel;
System.Data;
System.Drawing;
System.Linq;
System.Text;
System.Threading.Tasks;
System.Windows.Forms;
MySql.Data.MySqlClient;

namespace FormCrud
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Datamahasiswaamikom dma = new Datamahasiswaamikom();
private void Form1_Load(object sender, EventArgs e)
{
tampildata();
bersihkangrid();
}
void tampildata()
{
DataSet data = dma.gedData();
dataGridView1.DataSource= data;
dataGridView1.DataMember = "tbldataamikom";
}
private void button1_Click(object sender, EventArgs e)
{
data dt = new data();
dt.Nim = nim1.Text;
dt.Nama = nama.Text;
dt.Alamat = alamat.Text;
dt.Telpon = telpon.Text;
dma.Insert(dt);
MessageBox.Show ("Data berhasil disimpan",
"Pemberitahuan",MessageBoxButtons.OK);
tampildata();
bersihkangrid();
}
string nim;
private void button2_Click(object sender, EventArgs e)
{
data dt = new data();
dt.Nim = nim1.Text;
dt.Nama = nama.Text;
dt.Alamat = alamat.Text;
dt.Telpon = telpon.Text;
dma.edit(dt, nim);
MessageBox.Show("Data berhasil diubah", "Pemberitahuan", MessageBoxButtons.OK);
tampildata();
}

void bersihkangrid()
{
nim1.Text = "";

nama.Text = "";
alamat.Text = "";
telpon.Text = "";
nim1.Focus();
}
private void button3_Click(object sender, EventArgs e)
{
dma.hapus(nim);
MessageBox.Show("Data Berhasil dihapus","Pemberitahuan",MessageBoxButtons.OK);
tampildata();
nim1.Focus();
bersihkangrid();
}
private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
nim1.Text = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
nama.Text = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
alamat.Text = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();
telpon.Text = dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString();
nim = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
}
private void button4_Click(object sender, EventArgs e)
{
Close();
}
private void nim1_TextChanged(object sender, EventArgs e)
{
}
private void label1_Click(object sender, EventArgs e)
{
}
}

Anda mungkin juga menyukai