Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 4

Import database yg b kirim setelah selesai Add Reference trus buat module

koneksi.

KODING MODULE KONEKSI (Buat Module Koneksi dengan nama Koneksi)


Module Koneksi
Public strkoneksi As String = "server=localhost; user id=root;
password=''; database= karyawan"
End Module
KODING Form1 (Liat baik2 penempatan koding textbox dan combobox jgn sampe
salah, teliti baik2)

Imports MySql.Data.MySqlClient
Public Class Form1
Dim cn As New MySqlConnection(strkoneksi)
Dim perintah As New MySqlCommand
Dim cek As MySqlDataReader
Dim Data As New MySqlDataAdapter
Dim ds As New DataSet
Sub TampilData()
Dim cn As New MySqlConnection(strkoneksi)
cn.Open()
perintah.Connection = cn
perintah.CommandType = CommandType.Text
perintah.CommandText = "select * from data_karyawan"
Data.SelectCommand = perintah
ds.Tables.Clear()
Data.Fill(ds, "data_karyawan")
DataGridView1.DataSource = ds.Tables("data_karyawan")
cn.Close()
End Sub
Sub KosongData()
cn.Open()
perintah.Connection = cn
perintah.CommandType = CommandType.Text
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
TextBox4.Text = ""
ComboBox1.Text = ""
ComboBox2.Text = ""
ComboBox3.Text = ""
TextBox8.Text = ""
TextBox9.Text = ""
TextBox10.Text = ""
TextBox11.Text = ""
ComboBox4.Text = ""
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Call TampilData()
End Sub

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button4.Click
Me.Close()
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button1.Click
Dim cn As New MySqlConnection(strkoneksi)
cn.Open()
perintah.Connection = cn
perintah.CommandType = CommandType.Text
perintah.CommandText = "INSERT INTO
data_karyawan(id_karyawn,nik,nama_karyawan,ttl,jk,agama,jabatan,alamat,rt,rw,n
o_hp,pend) VALUES ('" & TextBox1.Text & "','" & TextBox2.Text & "','" &
TextBox3.Text & "','" & TextBox4.Text & "','" & ComboBox1.Text & "','" &
ComboBox2.Text & "','" & ComboBox3.Text & "','" & TextBox8.Text & "','" &
TextBox9.Text & "','" & TextBox10.Text & "','" & TextBox11.Text & "','" &
ComboBox4.Text & "')"
perintah.ExecuteNonQuery()
cn.Close()
MsgBox("Data Sukses Tersimpan", MsgBoxStyle.Information, "Pesan")
Call TampilData()
Call KosongData()
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button2.Click
Dim cn As New MySqlConnection(strkoneksi)
cn.Open()
perintah.Connection = cn
perintah.CommandType = CommandType.Text
perintah.CommandText = "UPDATE data_karyawan SET id_karyawn='" &
TextBox1.Text & "',nik='" & TextBox2.Text & "',nama_karyawan='" &
TextBox3.Text & "',ttl='" & TextBox4.Text & "',jk='" & ComboBox1.Text &
"',agama='" & ComboBox2.Text & "',jabatan='" & ComboBox3.Text & "',alamat='" &
TextBox8.Text & "',rt='" & TextBox9.Text & "',rw='" & TextBox10.Text &
"',no_hp='" & TextBox11.Text & "',pend='" & ComboBox4.Text & "' WHERE
id_karyawn ='" & TextBox1.Text & "'"
perintah.ExecuteNonQuery()
cn.Close()
MsgBox("Data Telah Diubah", MsgBoxStyle.Information, "Pesan")
Call TampilData()
Call KosongData()
End Sub

Private Sub DataGridView1_CellClick(ByVal sender As System.Object, ByVal e


As System.Windows.Forms.DataGridViewCellEventArgs) Handles
DataGridView1.CellContentClick
On Error Resume Next
Dim baris As Integer
With DataGridView1
baris = e.RowIndex
TextBox1.Text = .Rows(baris).Cells(0).Value
TextBox2.Text = .Rows(baris).Cells(1).Value
TextBox3.Text = .Rows(baris).Cells(2).Value
TextBox4.Text = .Rows(baris).Cells(3).Value
ComboBox1.Text = .Rows(baris).Cells(4).Value
ComboBox2.Text = .Rows(baris).Cells(5).Value
ComboBox3.Text = .Rows(baris).Cells(6).Value
TextBox8.Text = .Rows(baris).Cells(7).Value
TextBox9.Text = .Rows(baris).Cells(8).Value
TextBox10.Text = .Rows(baris).Cells(9).Value
TextBox11.Text = .Rows(baris).Cells(10).Value
ComboBox4.Text = .Rows(baris).Cells(11).Value
TextBox1.Enabled = True
TextBox1.Focus()
cn.Close()
Call TampilData()
End With
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
Dim cn As New MySqlConnection(strkoneksi)
cn.Open()
perintah.Connection = cn
perintah.CommandType = CommandType.Text
perintah.CommandText = "DELETE FROM data_karyawan WHERE id_karyawn='"
& TextBox1.Text & "'"
perintah.ExecuteNonQuery()
cn.Close()
MsgBox("Data Berhasil Dihapus", MsgBoxStyle.OkOnly Or
MsgBoxStyle.Information, "SUKSES")
Call TampilData()
Call KosongData()
End Sub
End Class

You might also like