Anda di halaman 1dari 5

KONEKSI DATABASE MYSQL

DENGAN VB NET 2008


1. Persiapan
- Instal XAMPP
- Intal Mysql-Connector
2. Database
Buat sebuah database baru dengan nama dbmahasiswa dan tabel baru dengan nama tbmahasiswa yang terdiri dari field-field berikut :
- Nama_Mahasiswa
tipe data
varchar (50)
- Semester
tipe data
char(5)
- Jurusan
tipe data
varchar(50)
Kemudian isikan dengan beberapa data.
3. Buat project baru di VB.net 2008 dan ikuti langkah-langkah berikut ini :
- Klik Project Add Reference MySQL.Data (pilih yang versi 2.050727) OK
- Lalu desain form berikut ini

4. Lalu buat sebuah modul untuk koneksi database kemudian ketikan perintah berikut :
Imports MySql.Data.MySqlClient
Module Module1

Public CONN As String = "Server=localhost;user id=root;database=dbmahasiswa"


End Module

5. Lalu ketikan pada form ketikan perintah berikut :


Imports MySql.Data.MySqlClient
Public Class Mahasiswa
Private Sub BERSIH()
TextBox1.Text = ""
TextBox2.Text = ""
ComboBox1.Text = "- Pilih -"
TextBox1.Focus()
End Sub
Private Sub TAMPILDATA()
Try
Using KONEKSI As New MySqlConnection(CONN)
Using CMD As New MySqlCommand("SELECT * FROM tbmahasiswa ", KONEKSI)
KONEKSI.Open()
Using DATA As MySqlDataReader = CMD.ExecuteReader
ListView1.Items.Clear()
Dim ListItem As ListViewItem
While DATA.Read
ListItem = New ListViewItem()
ListItem = ListView1.Items.Add(DATA("Nama_Mahasiswa"))
ListItem.SubItems.Add(DATA("Semester"))
ListItem.SubItems.Add(DATA("Jurusan"))
End While
End Using
End Using
End Using
Catch ex As Exception
MsgBox(ex.Message())
End Try
End Sub
Private Sub Mahasiswa_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TAMPILDATA()
BERSIH()
End Sub
Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles
TextBox1.KeyDown
If e.KeyCode = 13 Then
TextBox2.Focus()
End If
End Sub

Private Sub TextBox2_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles


TextBox2.KeyDown
If e.KeyCode = 13 Then
ComboBox1.Focus()
End If
End Sub
Private Sub ComboBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles
ComboBox1.KeyDown
If e.KeyCode = 13 Then
Button1.Focus()
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If TextBox1.Text = "" Then
MsgBox("Nama Mahasiswa Tidak Boleh Kosong ...!", MsgBoxStyle.Information, "Lengkapi Data")
TextBox1.Focus()
ElseIf TextBox2.Text = "" Then
MsgBox("Semester Tidak Boleh Kosong ...!", MsgBoxStyle.Information, "Lengkapi Data")
TextBox2.Focus()
ElseIf ComboBox1.Text = "" Then
MsgBox("Jurusan Tidak Boleh Kosong ...!", MsgBoxStyle.Information, "Lengkapi Data")
ComboBox1.Focus()
Else
If MsgBox("Yakin Data Ingin Disimpan !", MsgBoxStyle.YesNo, "Simpan Data") = MsgBoxResult.Yes Then
Try
Using KONEKSI As New MySqlConnection(CONN)
Using CMD As New MySqlCommand("INSERT INTO tbmahasiswa VALUES ('" & TextBox1.Text & _
"','" & TextBox2.Text & "','" & ComboBox1.Text & "')", KONEKSI)
KONEKSI.Open()
Using Data As MySqlDataReader = CMD.ExecuteReader
End Using
End Using
End Using
MsgBox("Data Sudah Berhasil Disimpan", MsgBoxStyle.Information, "Pesan")
BERSIH()
TAMPILDATA()
Catch ex As Exception
MsgBox(ex.Message())
End Try

Else

BERSIH()
End If
End If
End Sub

Private Sub ListView1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListView1.DoubleClick


Try
TextBox1.Text = ListView1.SelectedItems(0).Text
TextBox2.Text = ListView1.SelectedItems(0).SubItems(1).Text.ToString
ComboBox1.Text = ListView1.SelectedItems(0).SubItems(2).Text.ToString
Catch ex As Exception
End Try
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If MsgBox("Yakin Data Ingin Diubah !", MsgBoxStyle.YesNo, "Ubah Data !!!") = MsgBoxResult.Yes Then
Try
Using KONEKSI As New MySqlConnection(CONN)
Using CMD As New MySqlCommand("UPDATE tbmahasiswa SET Semester= '" & TextBox2.Text & _
"', Jurusan= '" & ComboBox1.Text & _
"' where Nama_Mahasiswa= '" & TextBox1.Text & "' ", KONEKSI)
KONEKSI.Open()
Using Data As MySqlDataReader = CMD.ExecuteReader
End Using
End Using
End Using
MsgBox("Data Sudah Berhasil Diubah", MsgBoxStyle.Information, "Pesan")
TAMPILDATA()
BERSIH()
Catch ex As Exception
MsgBox(ex.Message())
End Try
Else
BERSIH()
End If
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
If TextBox1.Text = "" Then
MsgBox("Pilih data terlebih dahulu....", MsgBoxStyle.Information, "Pilih Data")
Else
Try
Using KONEKSI As New MySqlConnection(CONN)
Using CMD As New MySqlCommand("DELETE * FROM tbmahasiswa WHERE Nama_Mahasiswa='" & _
TextBox1.Text & "'", KONEKSI)
KONEKSI.Open()
Using Data As MySqlDataReader = CMD.ExecuteReader
End Using
End Using
End Using
MsgBox("Data Sudah Berhasil Dihapus", MsgBoxStyle.Information, "Hapus Data User")
'Catch ex As Exception

'End Try
End If
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
BERSIH()
End Sub
End Class