by

ListView dan Database MySQL di Visual Basic

Listview merupakan salah satu item di Visual Basic VB.NET yang sering digunakan untuk menampilkan isi atau record database. Selain memiliki kemudahan dalam menggunakannya, tampilan Listview juga user friendly dan sederhana. Sehingga banyak programmer Windows Form menggunakannya sebagai item untuk menampilkan record database mereka. Berikut ini adalah contoh penggunaan Listview untuk insert, update, delete, dan view record menggunakan pemrograman Visual Basic VB.NET. (Project dan database dapat anda download lengkap melalui link yang disediakan).

Menggunakan ListView dan Database MySQL di Visual Basic
Menggunakan ListView dan Database MySQL di Visual Basic

Kode untuk koneksi “DTConnection.vb”, anda dapat menambahkannya melalui class/module dengan cara pilih “Project >> Add Class”. Kode ini digunakan untuk menghubungkan database dengan program Visual Basic VB.NET kita.

Kode: “DTConnection.vb

Imports MySql.Data.MySqlClient
Public Class DTConnection
    Dim conect As New MySqlConnection("server='localhost';user='root';password='';database='your_db'")
    Public Function open() As MySqlConnection
        Try
            conect.Open()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        Return conect
    End Function

    Public Function close() As MySqlConnection
        Try
            conect.Close()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        Return conect
    End Function
End Class

Selanjutnya kode untuk “Form1.vb”, anda bisa melihat semua detail/komponennya pada screenshot di postingan.

Kode: “Form1.vb

Imports MySql.Data.MySqlClient

Public Class Form1
#Region "declare"
    Dim mycmd As New MySqlCommand
    Dim myconnection As New DTConnection
    Dim objreader As MySqlDataReader
#End Region
    Private Sub BTINSERT_Click(sender As Object, e As EventArgs) Handles BTINSERT.Click
        mycmd.Connection = myconnection.open
        mycmd.CommandText = "insert into tb_testing(id,names) values('" & TID.Text & "','" & TNAMES.Text & "')"
        mycmd.ExecuteNonQuery()
        myconnection.close()
        MsgBox("Data added !!", MsgBoxStyle.Information, "Notice..")
    End Sub

    Private Sub BTUPDATE_Click(sender As Object, e As EventArgs) Handles BTUPDATE.Click
        mycmd.Connection = myconnection.open
        mycmd.CommandText = "update tb_testing set names='" & TNAMES.Text & "' where id='" & TID.Text & "'"
        mycmd.ExecuteNonQuery()
        myconnection.close()
        MsgBox("Data changed !!", MsgBoxStyle.Information, "Notice..")
    End Sub

    Private Sub BTDELETE_Click(sender As Object, e As EventArgs) Handles BTDELETE.Click
        mycmd.Connection = myconnection.open
        mycmd.CommandText = "delete from tb_testing where id='" & TID.Text & "'"
        mycmd.ExecuteNonQuery()
        myconnection.close()
        MsgBox("Data deleted !!", MsgBoxStyle.Information, "Notice..")
    End Sub

    Private Sub BTSHOW_Click(sender As Object, e As EventArgs) Handles BTSHOW.Click
        Dim isi As ListViewItem
        LISTSHOW.Items.Clear()
        mycmd.Connection = myconnection.open
        mycmd.CommandText = "select * from tb_testing"
        objreader = mycmd.ExecuteReader
        While objreader.Read
            isi = LISTSHOW.Items.Add(objreader.Item("id").ToString)
            isi.SubItems.Add(objreader.Item("names").ToString)
        End While
        myconnection.close()
    End Sub

    Private Sub LISTSHOW_SelectedIndexChanged(sender As Object, e As EventArgs) Handles LISTSHOW.SelectedIndexChanged
        Dim index As Integer
        If LISTSHOW.SelectedItems.Count = 0 Then Exit Sub
        With LISTSHOW
            index = .SelectedIndices(0)
            TID.Text = .Items(index).Text
            TNAMES.Text = .Items(index).SubItems(1).Text
        End With
        'Update sub
    End Sub
End Class

Jika masih kurang jelas, anda dapat melihat video dan mendownload project lengkapnya.

[ Download Project No. 1 ]

Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

2 comments