Koding koneksi VB.net dengan Database MySQL menggunakan PHP Triad
Imports MySql.Data.MySqlClient
Public Class Form1
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=akademik"
Try
db.ConnectionString = sql
db.Open()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Sub bersih()
NPM.Text = ""
Nama.Text = ""
Jenjang.Text = ""
Jurusan.Text = ""
rubahbutton(True, False, False, False, True)
NPM.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)
BtnBaru.Enabled = btn1
BtnSimpan.Enabled = btn2
BtnHapus.Enabled = btn3
BtnBatal.Enabled = btn4
Btnkeluar.Enabled = btn5
End Sub
Sub buattable()
LV.Columns.Add("NPM", 80, HorizontalAlignment.Center)
LV.Columns.Add("Nama", 180, HorizontalAlignment.Left)
LV.Columns.Add("Jenjang", 80, HorizontalAlignment.Left)
LV.Columns.Add("Jurusan", 100, HorizontalAlignment.Left)
LV.GridLines = True
LV.FullRowSelect = True
LV.View = View.Details
End Sub
Sub IsiTabel()
LV.Items.Clear()
sql = "select *from mahasiswa"
cmd = New MySqlCommand(sql, db)
rs = cmd.ExecuteReader
Try
While rs.Read
Dim lst As New ListViewItem
lst.Text = rs("NPM")
lst.SubItems.Add(rs("Nama"))
lst.SubItems.Add(rs("Jenjang"))
lst.SubItems.Add(rs("Jurusan"))
LV.Items.Add(lst)
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 mahasiswa(npm,nama,jurusan,jenjang)" & _
"values('" & NPM.Text & _
"','" & Nama.Text & _
"','" & Jenjang.Text & _
"','" & Jurusan.Text & "')"
pesan = "Data Telah Tersimpan"
Case 1
sql = "update mahasiswa set Nama='" & Nama.Text & "'," & _
"jenjang='" & Jenjang.Text & "'," & _
"jurusan='" & Jurusan.Text & "'" & _
"where npm ='" & NPM.Text & "'"
pesan = "Data Telah Terupdate"
Case 2
sql = "delete from mahasiswa where npm='" & NPM.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 showbd()
rs.Read()
NPM.Text = rs("npm")
Nama.Text = rs("nama")
Jenjang.Text = rs("Jenjang")
Jurusan.Text = rs("jurusan")
rubahbutton(False, True, True, True, False)
BtnSimpan.Text = "edit"
NPM.Enabled = False
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Call opendb()
Call bersih()
Call buattable()
Call IsiTabel()
End Sub
Private Sub BtnBaru_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnBaru.Click
Call bersih()
NPM.Focus()
End Sub
Private Sub BtnBatal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnBatal.Click
Call bersih()
NPM.Focus()
End Sub
Private Sub Btnkeluar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btnkeluar.Click
End
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 Akan dihapus", MsgBoxStyle.Information + MsgBoxStyle.YesNo, "hapus")
If x = vbYes Then
Call prosesdb(2)
End If
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 NPM_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles NPM.KeyPress
Dim tombol As Integer = Asc(e.KeyChar)
If tombol = 13 Then
Dim x As String
If NPM.Text = "" Then
MsgBox("isi NPM terlebih dahulu")
Else
sql = "select *from mahasiswa where.npm='" & "'"
cmd = New MySqlCommand(sql, db)
rs = cmd.ExecuteReader
Try
Call showbd()
Catch ex As Exception
x = NPM.Text
bersih()
NPM.Text = x
rubahbutton(False, True, False, True, False)
BtnSimpan.Text = "simpan"
End Try
Nama.Focus()
rs.Close()
End If
End If
End Sub
Private Sub NPM_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NPM.TextChanged
End Sub
End Class
Komentar
Posting Komentar