1. Aktifkan
aplikasi xampp terlebih dahulu.
2. Masuklah
pada browser lalu ke localhost/phpmyadmin, kemudian buat lah
sebuah database beserta dengan table-tabelnya, contoh sebagai berikut:
3. Kemudian
buatlah projek baru, dengan nama buku_crud (sesuaikan dengan nama yang
diinginkan).
4. Klik
project + Add references…+ Browse,
klik konektornya. Seperti berikut ini:
5. Kemudian
buatlah tampilan form seperti berikut
ini:
No
|
Objek
|
Property
|
Nilai
|
1.
|
Form
|
Name
Text
|
Form1
App
|
2.
|
Button
|
Name
Text
|
Btn_halamanbuku
Halaman Buku
|
3.
|
Button
|
Name
Text
|
Btn_halamankategori
Halaman Kategori
|
6. Klik
project + Add windows form…, lalu
buat nama form dengan “Form_Buku.vb”.
7. Kemudian
buatlah design nya, seperti berikut:
No
|
Objek
|
Property
|
Nilai
|
1.
|
Form
|
Name
Text
|
Form_Buku
Halaman Buku
|
2.
|
Button
|
Name
Text
|
Btn_tambahbuku
Tambah Buku
|
3.
|
Label
|
Name
Text
|
-
Cari Buku
|
4.
|
TextBox
|
Name
Text
|
Tb_cari
-
|
5.
|
Button
|
Name
Text
|
Btn_cari
Cari
|
6.
|
DataGridView
|
Name
Text
|
DG
-
|
7.
|
Button
|
Name
Text
|
Btn_kembali
Kembali
|
8. Klik
project + Add window form… buatlah
nama form “form_tambah_buku”, seperti
berikut:
9. Kemudian
buatlah design seperti berikut:
No
|
Objek
|
Property
|
Nilai
|
1.
|
Form
|
Name
Text
|
Form_Tambah_Buku
Form_Tambah_Buku
|
2.
|
Label
|
Name
Text
|
-
Judul
|
3.
|
Label
|
Name
Text
|
-
Kategori
|
4.
|
Label
|
Name
Text
|
-
ISBN
|
5.
|
Label
|
Name
Text
|
-
Penerbit
|
6.
|
Label
|
Name
Text
|
-
Penulis
|
7.
|
TextBox
|
Name
Text
|
Tb_judul
-
|
8.
|
ComboBox
|
Name
Text
DropDownStyle
|
Cb_kategori
-
DropDownList
|
9.
|
TextBox
|
Name
Text
|
Tb_isbn
-
|
10.
|
TextBox
|
Name
Text
|
Tb_penerbit
-
|
11.
|
TextBox
|
Name
Text
|
Tb_penulis
-
|
12.
|
Button
|
Name
Text
|
Btn_simpan
Simpan
|
13.
|
Button
|
Name
Text
|
Btn_batal
Batal
|
10. Klik
project + add module…, kemudian
buatlah nama modul “koneksi.vb”.
11. Kemudian
ketikkan source code berikut:
Imports MySql.Data.MySqlClient
Module koneksi
Public conn As MySqlConnection = New MySqlConnection("server='localhost';user='root';pwd='';database='pv_buku")
Public MyData As MySqlDataAdapter
Public MyCmd As MySqlCommand
Public idbuku As Integer
Public idkategori As Integer
Public Function GetResult(query
As String) As DataTable
Dim dt As New DataTable
conn.Open()
MyData = New MySqlDataAdapter
conn.Close()
Return dt
End Function
Public Sub Eksekusi(query As String)
conn.Open()
MyCmd = New MySqlCommand(query, conn)
MyCmd.ExecuteNonQuery()
conn.Close()
End Sub
End Module
12. Klik
project + add module…, kemudian beri
nama “prosedur.vb”, lalu ketikkan source
code berikut ini:
Module prosedur
Public Sub TampilBuku()
Dim query As String
Dim dt As New DataTable
query
= "SELECT idbuku AS 'ID Buku', Judul, ISBN, Kategori, Penerbit,
Penulis FROM buku NATURAL JOIN Kategori"
dt =
GetResult(query)
Form_Buku.DG.DataSource = dt
End Sub
Public Sub CariBuku(cari As String)
Dim query As String
Dim dt As New DataTable
query
= "SELECT idbuku AS 'ID Buku', Judul, ISBN, Kategori, Penerbit,
Penulis FROM buku NATURAL JOIN Kategori WHERE judul LIKE '&" & cari
& "%' OR kategori LIKE '%" & cari & "&' OR
penerbit LIKE '%" & cari & "%' OR penulis LIKE
'%" & cari & "%'"
dt =
GetResult(query)
Form_Buku.DG.DataSource = dt
End Sub
Public Sub
TambahBuku(judul As String, idkategori As Integer, isbn As String, penerbit As String, penulis As String)
Dim query As String = "INSERT INTO
buku VALUES (NULL, " & idkategori & ",'" & judul
& "','" & isbn & "','" & penerbit
& "','" & penulis & "')"
Eksekusi(query)
Form_Tambah_Buku.Close()
End Sub
Public Sub UbahBuku(idbuku
As Integer, judul As String, idkategori As Integer, isbn As String, penerbit As String, penulis As String)
Dim query As String = "UPDATE buku
idkategori= " & idkategori & ",judul='" & judul
& "',isbn='" & isbn & "',penerbit='" & penerbit
& "',penulis='" & penulis & "'WHERE
idbuku" & idbuku
Eksekusi(query)
Form_Detail_Buku.Close()
End Sub
Public Sub
HapusBuku(idbuku As Integer)
Dim query As String = "DELETE FROM
buku WHERE idbuku=" & idbuku
Eksekusi(query)
Form_Detail_Buku.Close()
End Sub
Public Sub
TampilKategori()
Dim query As String
Dim dt As New DataTable
query
= "SELECT idkategori as 'ID kategori', kategori FROM kategori"
dt =
GetResult(query)
kategori.DG.DataSource = dt
End Sub
Public Sub
CariKategori(cari As String)
Dim query As String
Dim dt As New DataTable
query
= "SELECT idkategori AS'ID Kategori', kategori FROM kategori WHERE
kategori LIKE '%" & cari & "%'"
dt =
GetResult(query)
kategori.DG.DataSource = dt
End Sub
Public Sub
TambahKategori(kategori As String)
Dim query As String = "INSERT INTO
kategori VALUES (NULL," & kategori & ",)"
Eksekusi(query)
Form_Tambah_Kategori.Close()
End Sub
Public Sub
UbahKategori(idkategori As Integer, kategori As String)
Dim query As String = "UPDATE
kategori SET kategori='" & kategori & "' WHERE
idkategori" & idkategori
Eksekusi(query)
Form_Detail_Kategori.Close()
End Sub
Public Sub
HapusKategori(idkategori As Integer)
Dim query As String = "DELETE FROM
kategori WHERE idkategori=" & idkategori
Eksekusi(query)
Form_Detail_Kategori.Close()
End Sub
End Module
Tidak ada komentar:
Posting Komentar