90 likes | 215 Views
Button Save Delete Edit. Sasa Ani Arnomo , S.Kom ., M.S.I. Buat databasenya lewat acces : Nama database => Database Mahasiswa Nama tabel => t blmahasiswa Kemudian rancang Form berikut :::::::. Masukkan tambahan header dipaling atas : Using System.Data.OleDb ;
E N D
Button Save Delete Edit SasaAniArnomo, S.Kom., M.S.I
Buatdatabasenyalewatacces: Nama database => Database Mahasiswa Namatabel => tblmahasiswa Kemudianrancang Form berikut:::::::
Masukkantambahan header dipalingatas: Using System.Data.OleDb; ================================== Tandamerah: alamatpenyimpanan database Ditulisdiataspublic Form1() ================================== string koneksi = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:/c#/Database Mahasiswa/Database Mahasiswa/Database Mahasiswa.accdb";
Untukmenampilkan database padaDataGridView, bisadilakukandengan 2 cara, yaitu : 1. Cara langsung Padadatagridview, kliktandasegitiga yang adapadapojokkananatasdatagridview. Lalupada Chose Data Source, pilih other data source, project data source, Nama database access, namatabel. 2. Menggunakansyntak Double klikpada form, lalumasukansyntakberikut private void Form1_Load(object sender, EventArgs e) { string sql = "SELECT * FROM tblmahasiswa";OleDbConnection con = new OleDbConnection(koneksi);con.Open();OleDbDataAdapter da = new OleDbDataAdapter(sql, con);DataSet ds = new DataSet();da.Fill(ds, "tblmahasiswa");con.Close(); dataGridView1.DataSource = ds.Tables["tblmahasiswa"].DefaultView; }
Kemudian double klikpadaTombolSimpandanisiSyntakseperti di bawahini private void button1_Click(object sender, EventArgs e) { try { string sql = string.Format("insert into tblMahasiswa (NRP,Nama,Kelas) VALUES ('{0}','{1}','{2}')", textBox1.Text, textBox2.Text, textBox3.Text); OleDbConnection conn = new OleDbConnection(koneksi);conn.Open();OleDbCommandcmd = new OleDbCommand(sql, conn);cmd.ExecuteNonQuery();conn.Close();MessageBox.Show("Data SudahDisimpan"); } catch (OleDbExceptionsalah) {MessageBox.Show(salah.ToString()); }}
TombolHapus private void button2_Click(object sender, EventArgs e) { try { string sql = string.Format("delete*from tblmahasiswa where nrp='" + textBox1.Text+ "'");OleDbConnection conn = new OleDbConnection(koneksi);conn.Open();OleDbCommandcmd = new OleDbCommand(sql, conn);cmd.ExecuteNonQuery();conn.Close();MessageBox.Show("Data TelahDihapus"); } catch (OleDbExceptionsalah) {MessageBox.Show(salah.ToString()); } }
Tombol Update/Edit private void button3_Click(object sender, EventArgs e) { try { string sql = string.Format("update tblmahasiswa set nama='" + textBox2.Text + "',kelas='" + textBox3.Text + "' where nrp='" + textBox1.Text + "'"); OleDbConnection conn = new OleDbConnection(koneksi);conn.Open();OleDbCommandcmd = new OleDbCommand(sql, conn);cmd.ExecuteNonQuery();conn.Close();MessageBox.Show("Perubahan Data SudahDisimpan"); } catch (OleDbException ex) {MessageBox.Show(ex.ToString()); }}
TombolCari private void button4_Click(object sender, EventArgs e) { OleDbDataReaderrdr = null; try { string sql = string.Format("select*from tblmahasiswa where nrp='" + textBox1.Text + "'");OleDbConnection conn = new OleDbConnection(koneksi);conn.Open();OleDbCommandcmd = new OleDbCommand(sql, conn);rdr=cmd.ExecuteReader(); if(rdr.Read()) { textBox2.Text =rdr["nama"].ToString(); textBox3.Text =rdr["kelas"].ToString(); } else {MessageBox.Show("Data TidakDitemukan"); } } catch(OleDbException ex) {MessageBox.Show(ex.ToString()); }}
TombolKeluar private void button4_Click(object sender, EventArgs e) { this.Close(); }