- Pertama-tama bukalah Ms.Access, buat database baru, pilih creat
- Tampilan saat membuat file lalu pilih design view
- Buat fiel dan data type
- Tampilan saat setelah selesai membuat nama field, kemudian pilih datasheetviw dan isi datanya
- Lalu buka visual studio, buat form masukkan textbox, label, datagridview, dan button pada form
- Pilih project, pilih add new data source, pilih database -> Next
- Pilih dataset ->Next
- Pilih file database yang akan dikoneksikan -> Next
- Kemudian Ceklist Table dan View -> Finish, database telah dimasukkan ke visual studio
- Kemudian codinglah seperti ini pada Form.cs
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 System.Data.OleDb;
namespace WindowsFormsApplication31
{
public partial class Form1 : Form
{
OleDbConnection konek = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=c:\users\rehab-rekons\documents\visual studio
2012\Projects\WindowsFormsApplication31\WindowsFormsApplication31\tugaspemvis6.mdb");
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
konek.Open();
string query = "Select *
from tabelmhs";
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
DataGridViewRow row = dataGridView1.Rows[e.RowIndex];
txtnim.Text =
row.Cells[0].Value.ToString();
txtnama.Text =
row.Cells[1].Value.ToString();
txtipk.Text =
row.Cells[2].Value.ToString();
txtket.Text = row.Cells[3].Value.ToString();
}
private void btnsimpan_Click(object sender, EventArgs e)
{
try
{
string sql = string.Format("insert into tabelmhs
values('{0}','{1}','{2}','{3}')",
txtnim.Text, txtnama.Text, txtipk.Text, txtket.Text);
OleDbCommand
perintah = new OleDbCommand(sql, konek);
perintah.ExecuteNonQuery();
MessageBox.Show("Data berhasil di simpan");
perintah.Dispose();
}
catch (Exception)
{
MessageBox.Show("Data Gagal Disimpan");
}
}
private void btncari_Click(object sender, EventArgs e)
{
OleDbDataReader baca = null;
try
{
string sql = string.Format("select * from tabelmhs where NIM='" + txtnim.Text + "'");
OleDbCommand
perintah = new OleDbCommand(sql, konek);
baca =
perintah.ExecuteReader();
if (baca.Read())
{
txtnim.Text = baca["NIM"].ToString();
txtnama.Text = baca["Nama"].ToString();
txtipk.Text = baca["IPK"].ToString();
txtket.Text = baca["Keterangan"].ToString();
}
else
{
MessageBox.Show("Data Tidak Ditemukan");
}
}
catch (OleDbException ex)
{
MessageBox.Show(ex.ToString());
}
}
private void btnedit_Click(object sender, EventArgs e)
{
try
{
string sql = string.Format("update tabelmhs set NIM='" + txtnim.Text + "',Nama='" + txtnama.Text + "',IPK='" + txtipk.Text + "',Keterangan='" + txtket.Text + "'where
nim ='" + txtnim.Text + "'");
OleDbCommand
perintah = new OleDbCommand(sql, konek);
perintah.ExecuteNonQuery();
MessageBox.Show("Data berhasil diedit");
perintah.Dispose();
}
catch (Exception)
{
MessageBox.Show("Data Gagal Diedit");
}
}
private void btnhapus_Click(object sender, EventArgs e)
{
try
{
string sql = string.Format("delete from tabelmhs where NIM='" + txtnim.Text + "'");
OleDbCommand
perintah = new OleDbCommand(sql, konek);
perintah.ExecuteNonQuery();
MessageBox.Show("Data berhasil dihapus");
perintah.Dispose();
}
catch (Exception)
{
MessageBox.Show("Data Gagal Dihapus");
}
}
private void btnref_Click(object sender, EventArgs e)
{
try
{
string query = "select * from tabelmhs";
OleDbCommand
perintah = new OleDbCommand(query, konek);
DataSet ds = new DataSet();
OleDbDataAdapter
adapter = new OleDbDataAdapter(perintah);
adapter.Fill(ds, "res");
dataGridView1.DataSource =
ds.Tables["res"];
adapter.Dispose();
perintah.Dispose();
}
catch (Exception)
{
MessageBox.Show("Gagal menampilkan data");
}
}
}
}
|
- Kemudian akan tampil output seperti berikut ini
Gambar
1. Tampilan awal
Gambar
2. Setelah ditekan tombol refresh
Gambar
3. Tapilan saat menekan tombol simpan
dan refresh
Gambar
4. Tampilan saat memilih data kemudian menekan tombol hapus dan refresh
Gambar
5. Tampilan saat menulis NIM kemudian menekan tombol cari
Tidak ada komentar:
Posting Komentar