Anda di halaman 1dari 12

[Koneksi.

cs]

using System;
using MySql.Data.MySqlClient;
namespace mahasiswa
{
/// <summary>
/// Description of Koneksi.
/// </summary>
public class Koneksi
{
string alamat="SERVER=localhost;DATABASE=db_latihan_visual;UID=root;PASSWORD=;";
public MySqlConnection koneksi;

public void buka(){


koneksi= new MySqlConnection(alamat);
koneksi.Open();
}
public void tutup(){
koneksi= new MySqlConnection(alamat);
koneksi.Open();
}
}
}

[ViewData.cs]

using System;
using System.Data;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace mahasiswa
{
/// <summary>
/// Description of ViewData.
/// </summary>
public class ViewData
{
MySqlCommand query;
Koneksi sambung;
MySqlDataAdapter adapter;
string sql;
DataTable tabel;

//tampilkan semua mahasiswa di tabel


public DataTable bacasemua(){
sambung = new Koneksi();
sql="select * from mahasiswa order by stb asc";
tabel=new DataTable();
try{
sambung.buka();
query=new MySqlCommand(sql, sambung.koneksi);
adapter=new MySqlDataAdapter(query);
query.ExecuteNonQuery();
adapter.Fill(tabel);
}catch(Exception er)
{ MessageBox.Show(er.Message
);
}
sambung.tutup();
return tabel;
}

//mencari data berdasarkan nama


public DataTable search(string nama){
sambung = new Koneksi();
sql="select * from mahasiswa where nama like '%"+nama+"%'";
tabel=new DataTable();
try{
sambung.buka();
query=new MySqlCommand(sql, sambung.koneksi);
adapter=new MySqlDataAdapter(query);
query.ExecuteNonQuery();
adapter.Fill(tabel);
}catch(Exception er)
{ MessageBox.Show(er.Message
);
}
sambung.tutup();
return tabel;
}
}
}
[Login.cs]

using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace mahasiswa
{
/// <summary>
/// Description of Login.
/// </summary>
public partial class Login : Form
{
MySqlCommand query;
Koneksi sambung;
MySqlDataAdapter adapter;
string sql;
DataTable tabel;

public static string namasession="";


public static string idsession="";

string id, username, password,nama;


public Login()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();

//
// TODO: Add constructor code after the InitializeComponent() call.
//
}

void btnLogin(object sender, EventArgs e)


{
if(textBox1.Text=="" & textBox2.Text==""){
MessageBox.Show("Silahkan Isi Username dan Password","Login Invalid");
}else{
try{
sambung = new Koneksi();
sambung.buka();
sql="select * from user where username='"
+textBox1.Text.Replace("\"",string.Empty).Replace("'",string.Empty)
+"' and
password='"+textBox2.Text.Replace("\"",string.Empty).Replace("'",string.Empty)+"'";

MySqlDataReader row;
query=new MySqlCommand(sql,sambung.koneksi);
adapter=new MySqlDataAdapter(query);
row=query.ExecuteReader();
if(row.HasRows){
while(row.Read()){
id = row["id"].ToString();
username = row["username"].ToString();
nama = row["nama"].ToString();
password = row["password"].ToString();
idsession=id;
namasession=nama;

}
MainForm master = new MainForm();
master.Show();
this.Hide();
}else{
MessageBox.Show("Username Or Password Not Valid","Login Invalid");
}

}catch(Exception er)
{ MessageBox.Show(er.Message,"Error"
);
}
}
}
}
}

[MainForm.cs]

using System;
using System.Data;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace mahasiswa
{
/// <summary>
/// Description of MainForm.
/// </summary>
public partial class MainForm : Form
{
MySqlCommand query;
Koneksi sambung;
MySqlDataAdapter adapter;
string sql;
DataTable tabel;
public MainForm()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();
TampilDalamTabel();
//
// TODO: Add constructor code after the InitializeComponent() call.
//
}
void TampilDalamTabel(){
ViewData tampilkan=new ViewData();
DataTable tabel=new DataTable();

tabel=tampilkan.bacasemua();
dataGridView1.DataSource=tabel;
dataGridView1.Columns[0].HeaderText="STB";
dataGridView1.Columns[1].HeaderText="Nama";
dataGridView1.Columns[2].HeaderText="Alamat";
dataGridView1.Columns[3].HeaderText="Nilai Tugas";
dataGridView1.Columns[4].HeaderText="Nilai MID";
dataGridView1.Columns[5].HeaderText="Nilai Final";
dataGridView1.Columns[6].HeaderText="Nilai Akhir";
dataGridView1.Columns[7].HeaderText="Nilai Huruf";
dataGridView1.Columns[8].HeaderText="Keterangan";
}

//tambah data
void Insert(object sender, EventArgs e)
{
sambung = new Koneksi();
sql="insert into mahasiswa (stb, nama, alamat, nilai_tugas, nilai_mid, nilai_final,
nilai_akhir,nilai_huruf,keterangan) values ('"+
textBox1.Text+"', '"+textBox2.Text+"', '"+textBox3.Text+"', '"+int.Parse(textBox4.Text)
+"','"+int.Parse(textBox5.Text)+"','"+int.Parse(textBox6.Text)+"','"+doubl e.Parse(textBox7.Text)
+"','"+
textBox8.Text+"','"+textBox9.Text+"')";

try{
sambung.buka();
query=new MySqlCommand(sql,sambung.koneksi);
adapter=new MySqlDataAdapter(query);
query.ExecuteNonQuery();
TampilDalamTabel();
MessageBox.Show("Data has been saved!","192011");

}catch (Exception er)


{ MessageBox.Show(er.Message
);
}
sambung.tutup();
}

//tampilkan hasil pencarian nama


void SrcResult(object sender, EventArgs e){
ViewData tampilkan=new ViewData();
DataTable tabel=new DataTable();

tabel=tampilkan.search(textBox10.Text);
dataGridView1.DataSource=tabel;
}

void DataGridView1CellClick(object sender, DataGridViewCellEventArgs e)


{
int stb=e.RowIndex;

textBox1.Enabled=false;
DataGridViewRow selectedRow=dataGridView1.Rows[stb];
textBox1.Text=selectedRow.Cells[0].Value.ToString();
textBox2.Text=selectedRow.Cells[1].Value.ToString();
textBox3.Text=selectedRow.Cells[2].Value.ToString();
textBox4.Text=selectedRow.Cells[3].Value.ToString();
textBox5.Text=selectedRow.Cells[4].Value.ToString();
textBox6.Text=selectedRow.Cells[5].Value.ToString();
textBox7.Text=selectedRow.Cells[6].Value.ToString();
textBox8.Text=selectedRow.Cells[7].Value.ToString();
textBox9.Text=selectedRow.Cells[8].Value.ToString();
}

void Reset(object sender, EventArgs e)


{
textBox1.Enabled=true;
textBox1.Text="";
textBox2.Text="";
textBox3.Text="";
textBox4.Text="";
textBox5.Text="";
textBox6.Text="";
textBox7.Text="";
textBox8.Text="";
textBox9.Text="";

TampilDalamTabel();
}

void Update(object sender, EventArgs e)


{
sambung = new Koneksi();
sql="update mahasiswa set nama='"+textBox2.Text+"', alamat='"+textBox3.Text+"',
nilai_tugas='"+int.Parse(textBox4.Text)+"', nilai_mid='"+int.Parse(textBox5.Text)+"',
nilai_final='"+int.Parse(textBox6.Text)+"', nilai_akhir='"+double.Parse(textBox7.Text)
+"',nilai_huruf='"+textBox8.Text+"',keterangan='"+textBo x9.Text+"' where
stb='"+textBox1.Text+"'";

try{
sambung.buka();
query=new MySqlCommand(sql,sambung.koneksi);
adapter=new MySqlDataAdapter(query);
query.ExecuteNonQuery();
TampilDalamTabel();
textBox1.Enabled=true;
textBox1.Text="";
textBox2.Text="";
textBox3.Text="";
textBox4.Text="";
textBox5.Text="";
textBox6.Text="";
textBox7.Text="";
textBox8.Text="";
textBox9.Text="";
MessageBox.Show("Data has been updated !","192011");

}catch (Exception er)


{ MessageBox.Show(er.Message
);
}
sambung.tutup();
}

void Delete(object sender, EventArgs e)


{
sambung = new Koneksi();
sql="delete from mahasiswa where stb='"+textBox1.Text+"'";

try{
sambung.buka();
query=new MySqlCommand(sql,sambung.koneksi);
adapter=new MySqlDataAdapter(query);
query.ExecuteNonQuery();
TampilDalamTabel();
textBox1.Text="";
textBox2.Text="";
textBox3.Text="";
textBox4.Text="";
textBox5.Text="";
textBox6.Text="";
textBox7.Text="";
textBox8.Text="";
textBox9.Text="";
textBox1.Enabled=true;
MessageBox.Show("Data success Deleted !","192011");

}catch (Exception er)


{ MessageBox.Show(er.Message
);
}
sambung.tutup();

void Hitung(object sender, EventArgs e)


{
try{
int tugas, mid, final, akhir;

tugas = int.Parse(textBox4.Text);
mid = int.Parse(textBox5.Text);
final = int.Parse(textBox6.Text);
akhir = Convert.ToInt32((0.3 * tugas) + (0.3 * mid) + (0.4 * final));
textBox7.Text = akhir.ToString();
if (akhir <= 35)
{
textBox8.Text = "E";
textBox9.Text = "Error";
}
else if (akhir <= 50)
{
textBox8.Text = "D";
textBox9.Text = "Kurang";
}
else if (akhir <= 65)
{
textBox8.Text = "C";
textBox9.Text = "Cukup";
}
else if (akhir <= 80)
{
textBox8.Text = "B";
textBox9.Text = "Baik";
}
else
{
textBox8.Text = "A";
textBox9.Text = "Sangat Baik";
}
}catch(Exception er)
{ MessageBox.Show(er.Message,"Error"
);
}
}
//keluar windows/tutup app
void keluar(object sender, EventArgs e)
{
this.Close();
}
}
}
GANTI DATA DARI STB 172157

HAPUS STB 124356


Cari data bernama pord

Anda mungkin juga menyukai