Imports MySql.Data.MySqlClient
Public Class Form3
Public db As New
MySql.Data.MySqlClient.MySqlConnection
Public sql As String
Public cmd As MySqlCommand
Public rs As MySqlDataReader
Sub
opendb()
sql = "server=localhost;uid=root;pwd;database=barang"
Try
db.ConnectionString = sql
db.Open()
Catch
ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Sub
bersih()
kode.Text = ""
nama.Text = ""
alamat.Text = ""
tlpn.Text = ""
penghubung.Text = ""
pemimpin.Text = ""
RUBAHBUTTON(True,
False, False, False, False, True)
kode.Enabled = True
End Sub
Sub
RUBAHBUTTON(ByVal Btn1 As
Boolean, ByVal
Btn2 As Boolean,
ByVal Btn3 As Boolean, ByVal Btn4 As Boolean, ByVal Btn5 As Boolean, ByVal Btn6 As Boolean)
Btnbaru.Enabled = Btn1
Btnsimpan.Enabled = Btn2
Btnedit.Enabled = Btn3
Btnhapus.Enabled = Btn4
Btnbatal.Enabled = Btn5
Btnkeluar.Enabled = Btn6
End Sub
Sub
buattabel()
Lv.Columns.Add("kode", 80,
HorizontalAlignment.Center)
lv.Columns.Add("nama",
180, HorizontalAlignment.Left)
Lv.Columns.Add("alamat", 80,
HorizontalAlignment.Left)
Lv.Columns.Add("tlpn", 80, HorizontalAlignment.Center)
lv.Columns.Add("penghubung", 180,
HorizontalAlignment.Left)
Lv.Columns.Add("pemimpin", 80,
HorizontalAlignment.Left)
lv.GridLines = True
lv.FullRowSelect = True
lv.View = View.Details
End Sub
Sub
ISITABEL()
lv.Items.Clear()
sql = "select
* from data_supplier"
cmd = New
MySqlCommand(sql, db)
rs = cmd.ExecuteReader
Try
While
rs.Read
Dim
ist As New
ListViewItem
ist.Text = rs("kode")
ist.SubItems.Add(rs("nama"))
ist.SubItems.Add(rs("alamat"))
ist.SubItems.Add(rs("tlpn"))
ist.SubItems.Add(rs("penghubung"))
ist.SubItems.Add(rs("pemimpin"))
lv.Items.Add(ist)
End
While
Catch
ex As Exception
MsgBox(ex.Message)
End Try
rs.Close()
End Sub
Sub
prosesdb(ByVal log As
Integer)
Dim
pesan As String
= ""
Select Case log
Case
0
sql = "insert
into data_supplier(kode, nama, alamat,
tlpn, penghubung, pemimpin)" & _
"values
('" & kode.Text & _
"','"
& nama.Text & _
"','" & alamat.Text & _
"','" & tlpn.Text & _
"','" & penghubung.Text & _
"','"
& pemimpin.Text & "')"
pesan = "data telah disimpan"
Case
1
sql = "update
pesanan set nama='" & nama.Text & "',
" & _
"harga='" & harga.Text & "'," & _
"where
kode='" & kode.Text & "'"
pesan = "telah di update"
Case
2
sql = "delete
from data_supplier where kode='" & kode.Text & "'"
pesan = "data telah dihapus"
End Select
Try
cmd = New
MySqlCommand(sql, db)
cmd.ExecuteNonQuery()
MsgBox(pesan,
MsgBoxStyle.Information + MsgBoxStyle.OkOnly, "komfirmasi")
Call
bersih()
Call
ISITABEL()
Catch
ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Sub
showdb()
rs.Read()
kode.Text = rs("kode")
nama.Text = rs("nama")
alama.Text = rs("alama")
tlpn.Text = rs("tlpn")
penghubung.Text = rs("penghubung")
pemimpin.Text = rs("pemimpin")
RUBAHBUTTON(False,
False, True, True, True, False)
Btnedit.Text = ""
kode.Enabled = False
End Sub
Private Sub Form3_Load(ByVal
sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Call
opendb()
Call
bersih()
Call
ISITABEL()
Call
buattabel()
End Sub
Private Sub Btnbaru_Click(ByVal
sender As System.Object, ByVal e As
System.EventArgs) Handles Btnbaru.Click
Call
bersih()
kode.Focus()
End Sub
Private Sub Btnedit_Click(ByVal
sender As System.Object, ByVal e As
System.EventArgs) Handles Btnedit.Click
If
Btnedit.Text = "edit" Then prosesdb(0) Else
prosesdb(1)
End Sub
Private Sub Btnhapus_Click(ByVal
sender As System.Object, ByVal e As
System.EventArgs) Handles Btnhapus.Click
Dim x As String
x = MsgBox("anda
yakin ingin menghapus", MsgBoxStyle.Information +
MsgBoxStyle.YesNo, "hapus")
If x =
vbYes Then
Call
prosesdb(2)
End If
End Sub
Private Sub Btnkeluar_Click(ByVal
sender As System.Object, ByVal e As
System.EventArgs) Handles Btnkeluar.Click
Me.Close()
End Sub
Private Sub Btnbatal_Click(ByVal
sender As System.Object, ByVal e As
System.EventArgs) Handles Btnbatal.Click
Call
bersih()
kode.Focus()
End Sub
Private Sub Btnsimpan_Click(ByVal
sender As System.Object, ByVal e As
System.EventArgs) Handles Btnsimpan.Click
If
Btnsimpan.Text = "simpan" Then prosesdb(0) Else
prosesdb(1)
End Sub
Private Sub Kode_KeyPress(ByVal
sender As Object,
ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles
kode.KeyPress
Dim
tombol As Integer
= Asc(e.KeyChar)
If
tombol = 13 Then
Dim x As String
If
kode.Text = "" Then
MsgBox("isi
kode terlebih dahulu")
Else
sql = "select*
from data_supplier where kode='" & kode.Text & "'"
cmd = New
MySqlCommand(sql, db)
rs = cmd.ExecuteReader
Try
Call
showdb()
Catch
ex As Exception
x = kode.Text
bersih()
kode.Text = x
RUBAHBUTTON(False, True, False, False, True, False)
Btnsimpan.Text = ""
End
Try
nama.Focus()
rs.Close()
End
If
End If
End Sub
End Class
Demikian lah postingan saya Nama : Dewi Juliana Npm : 1102085 Kls : MI/P/1101
Selamat Belajar VB Net
Tidak ada komentar:
Posting Komentar