Crud merupakan basic awal yang untuk mempelajari bahasa pemograman yang menggunakan database.
Crud sendiri kepanjangan dari Create Read Update Delete,
Sebelum membahas
Crud lebih jauh pastinya harus sudah paham cara koneksi ke database, baiklah jika belum paham mengenai koneksi database silahkan baca artikel ini
Cara Koneksi ke Database
Oke kita langsung saja membahas cara membuat
Crud, Database yang digunakan yaitu Mysql xampp.
1. Persiapan database, buatlah database dengan nama db_crud dan tambahkan tabel dengan properti sebagai berikut
2. Buatlah class Koneksi berinama koneksi.vb
Imports Microsoft.VisualBasic
Imports MySql.Data.MySqlClient
Public Class koneksi
Public str As String
Public conn As MySqlConnection
Public cmd As New MySqlCommand
Public da As MySqlDataAdapter
Public ds As DataSet
Public dt As DataTable
Public rd As MySqlDataReader
Public Function openconn() As Boolean
Try
conn = New MySqlConnection("server=localhost;user id=root;database=db_crud")
conn.Open()
If conn.State <> ConnectionState.Open Then
Return False
Else
Return True
End If
Catch ex As Exception
End Try
End Function
Public Sub closeconn()
If Not IsNothing(conn) Then
conn.Close()
conn = Nothing
End If
End Sub
Public Function cek_koneksi()
If Not openconn() Then
MsgBox("Koneksi Gagal")
Else
MsgBox("Koneksi Sukses")
End If
Return 0
End Function
Public Function ExecuteQuery(ByVal Query As String) As DataTable
If Not openconn() Then
MsgBox("koneksi gagal")
Return Nothing
Exit Function
End If
cmd = New MySqlCommand(Query, conn)
da = New MySqlDataAdapter
da.SelectCommand = cmd
ds = New DataSet
da.Fill(ds)
dt = ds.Tables(0)
Return dt
dt = Nothing
ds = Nothing
da = Nothing
closeconn()
End Function
Public Sub ExecuteNonQuery(ByVal Query As String)
' Try
If Not openconn() Then
MsgBox("Gagal Koneksi")
Exit Sub
End If
'On Error Resume Next
cmd = New MySqlCommand
cmd.Connection = conn
cmd.CommandType = CommandType.Text
cmd.CommandText = Query
cmd.ExecuteNonQuery()
cmd = Nothing
closeconn()
Exit Sub
' Catch ex As Exception
' End Try
End Sub
Public Sub ExecuteReader(ByVal Query As String)
If Not openconn() Then
MsgBox("Gagal Koneksi")
Exit Sub
End If
cmd = New MySqlCommand(Query, conn)
rd = cmd.ExecuteReader()
rd.Read()
End Sub
End Class
3. Buatlah form baru beri nama form1 kemudian sisipkan 4 textbox, 4 Label, 4 button dan satu datagridview, kemudian atur propertinya menjadi seperti pada gambar dibawah ini
Sisipkan Kode dibawah ini
Public Class Form1
Dim proses As New koneksi
Dim sql As String
Dim datatable As DataTable
Private Sub Simpan() 'Simpan Data
sql = "insert into user values ('" & txt_id.Text & "','" & txt_nama.Text & "','" & txt_alamat.Text & "','" & txt_no_telepon.Text & "') "
proses.ExecuteNonQuery(sql)
MsgBox("Data Berhasil disimpan")
End Sub
Private Sub ubah() ' Ubah Data
sql = "select * from user where id='" & txt_id.Text & "'"
datatable = proses.ExecuteQuery(sql)
If datatable.Rows.Count <> 0 Then
sql = "update user set nama='" & txt_nama.Text & "',alamat='" & txt_alamat.Text & "',no_telepon='" & txt_no_telepon.Text & "' where id='" & txt_id.Text & "'"
proses.ExecuteNonQuery(sql)
MsgBox("Data Berhasil diubah")
Else
MsgBox("Data tidak ditemukan")
End If
End Sub
Private Sub hapus() 'Hapus Data
sql = "select * from user where id='" & txt_id.Text & "'"
datatable = proses.ExecuteQuery(sql)
If datatable.Rows.Count <> 0 Then
sql = "delete from user where id='" & txt_id.Text & "'"
proses.ExecuteNonQuery(sql)
MsgBox("Data Berhasil dihapus")
Else
MsgBox("Data tidak ditemukan")
End If
End Sub
Private Sub tampil() 'Tampil Data
sql = "select * from user"
datatable = proses.ExecuteQuery(sql)
If datatable.Rows.Count <> 0 Then
DataGridView1.DataSource = datatable
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If txt_id.Text = "" Or txt_nama.Text = "" Or txt_alamat.Text = "" Or txt_no_telepon.Text = "" Then
MsgBox("Data Belum lengkap")
Else
Simpan()
MsgBox("Data Berhasil disimpan")
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If txt_id.Text = "" Or txt_nama.Text = "" Or txt_alamat.Text = "" Or txt_no_telepon.Text = "" Then
MsgBox("Data Belum lengkap")
Else
ubah()
End If
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
If txt_id.Text = "" Then
MsgBox("Id Masih kosong")
Else
hapus()
End If
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
tampil()
End Sub
End Class
Crud sudah berhasil dibuat, jika ada yang eror atau ada yang mau ditanyakan silahkan tanyakan saya akan menjawab langsung, Jangan lupa tunggu tutorial berikutnya yaitu Cara membuat Login