Rancang Form seperti gambar berikut
sesuaiin name2 nya ke SC di bawah
Berikut Source Code nya
Imports MySql.Data.MySqlClient
Public Class Form3
Dim db As New MySql.Data.MySqlClient.MySqlConnection
Dim cmd As MySqlCommand
Dim rs As MySqlDataReader
Dim sql As String
Sub opendb()
sql = "server=localhost;uid=root;pwd=;database=kampus"
Try
db.ConnectionString = sql
db.Open()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Sub Rubah(ByVal T1 As Boolean, ByVal T2 As Boolean, ByVal T3 As Boolean, ByVal T4 As Boolean, ByVal T5 As Boolean, ByVal T6 As Boolean)
baru.Enabled = T1
simpan.Enabled = T2
edit.Enabled = T3
hapus.Enabled = T4
batal.Enabled = T5
keluar.Enabled = T6
End Sub
Sub BuatTable()
lv.Columns.Add("NIDN", 80, HorizontalAlignment.Center)
lv.Columns.Add("Nama", 120, HorizontalAlignment.Center)
lv.Columns.Add("Keahlian", 80, HorizontalAlignment.Center)
lv.Columns.Add("Alamat", 80, HorizontalAlignment.Center)
lv.Columns.Add("Telp", 80, HorizontalAlignment.Center)
lv.View = View.Details
lv.GridLines = True
lv.FullRowSelect = True
End Sub
Sub isitabel()
lv.Items.Clear()
sql = "select * from dosen"
cmd = New MySqlCommand(sql, db)
rs = cmd.ExecuteReader
Try
While rs.Read
Dim baris As New ListViewItem
baris.Text = rs("NIDN")
baris.SubItems.Add(rs("Nama"))
baris.SubItems.Add(rs("Keahlian"))
baris.SubItems.Add(rs("Alamat"))
baris.SubItems.Add(rs("Telp"))
lv.Items.Add(baris)
End While
Catch ex As Exception
MsgBox(ex.Message)
End Try
rs.Close()
End Sub
Sub bersih()
NIDN.Text = ""
Nama.Text = ""
Keahlian.Text = ""
Alamat.Text = ""
Tlp.Text = ""
isitabel()
Rubah(True, False, False, False, False, True)
End Sub
Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
opendb()
BuatTable()
isitabel()
bersih()
End Sub
Private Sub baru_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles baru.Click
bersih()
Rubah(False, True, False, False, True, False)
NIDN.Focus()
End Sub
Private Sub keluar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles keluar.Click
Me.Close()
End Sub
Private Sub batal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles batal.Click
bersih()
NIDN.Focus()
Rubah(False, True, False, False, True, False)
End Sub
Private Sub simpan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles simpan.Click
sql = "INSERT INTO dosen VALUES('" & NIDN.Text & " ',' " & Nama.Text & " ',' " & Keahlian.Text & " ',' " & Alamat.Text & " ',' " & Tlp.Text & " ')"
Try
cmd = New MySqlCommand(sql, db)
cmd.ExecuteNonQuery()
Catch ex As Exception
MsgBox(ex.Message)
End Try
bersih()
NIDN.Focus()
End Sub
Private Sub edit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles edit.Click
sql = "UPDATE dosen SET nama='" & Nama.Text & "', keahlian='" & Keahlian.Text & "', alamat='" & Alamat.Text & "', telp='" & Tlp.Text & "' where nidn='" & NIDN.Text & "'"
Try
cmd = New MySqlCommand(sql, db)
cmd.ExecuteNonQuery()
Catch ex As Exception
MsgBox(ex.Message)
End Try
bersih()
NIDN.Focus()
End Sub
Private Sub hapus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles hapus.Click
sql = "DELETE FROM dosen where nidn='" & NIDN.Text & "'"
Try
cmd = New MySqlCommand(sql, db)
cmd.ExecuteNonQuery()
Catch ex As Exception
MsgBox(ex.Message)
End Try
bersih()
NIDN.Focus()
End Sub
Private Sub NIDN_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles NIDN.KeyPress
If Asc(e.KeyChar) = 13 Then
If NIDN.Text = "" Then
MsgBox("Isi dulu NIDN nya mas bro")
Else
sql = "SELECT * FROM dosen where nidn='" & NIDN.Text & "'"
cmd = New MySqlCommand(sql, db)
rs = cmd.ExecuteReader
Try
rs.Read()
Nama.Text = rs("Nama")
Keahlian.Text = rs("Keahlian")
Alamat.Text = rs("Alamat")
Tlp.Text = rs("Telp")
Rubah(False, False, True, True, True, False)
Catch ex As Exception
Dim x As String
x = NIDN.Text
bersih()
NIDN.Text = x
Rubah(False, True, False, False, True, False)
End Try
rs.Close()
Nama.Focus()
End If
End If
End Sub
Private Sub NIDN_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NIDN.TextChanged
End Sub
End Class
saya tidak mau terlalu membahas nya karena takut juga di copas :)
kasihan kita yang share nya
Membuat Koneksi VB.Net dengan MySQL, Serta Select, Add, Delete, Update dan listing Program nya
4/
5
Oleh
Holong.Me