Senin, 11 Juli 2016

Cara Menampilkan Database Menggunakan ListView di Visual Basic

Berikut panduan bagaimana cara menampilkan record database menggunakan listview di Visual Basic. Anda perlu membuat class untuk koneksi data dan sebuah form untuk menempatkan listview beserta item yang lainnya. Agar lebih jelas anda dapat mendownload project maupun melihat video panduannya.
Kode Koneksi:

Imports MySql.Data.MySqlClient
Namespace accessdata
    Public Class koneksidata
        Dim conect As New MySqlConnection("server=localhost;uid=root;pwd='';database='skyars'")
        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
End Namespace
Kode Form1:

Imports MySql.Data.MySqlClient
Public Class Form1
#Region "deklarasi"
    Dim mycmd As New MySqlCommand
    Dim myconnection As New accessdata.koneksidata
    Dim objreader As MySqlDataReader
#End Region
#Region "prosedur"
    Sub bacalistview()
        Dim isi As ListViewItem
        LISTFORM.Items.Clear()
        mycmd.Connection = myconnection.open
        mycmd.CommandText = "select * from tbtes"
        objreader = mycmd.ExecuteReader
        While objreader.Read
            isi = LISTFORM.Items.Add(objreader.Item("nomor").ToString)
            isi.SubItems.Add(objreader.Item("nama").ToString)
        End While
        myconnection.close()
    End Sub
    Sub kliklistview()
        Dim index As Integer
        If LISTFORM.SelectedItems.Count = 0 Then Exit Sub
        With LISTFORM
            index = .SelectedIndices(0)
            TNO.Text = .Items(index).Text
            TNAMA.Text = .Items(index).SubItems(1).Text
        End With
    End Sub
#End Region
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        mycmd.Connection = myconnection.open
        mycmd.CommandText = "insert into tbtes(nomor,nama) values('" & (TNO.Text) & "','" & (TNAMA.Text) & "')"
        mycmd.ExecuteNonQuery()
        myconnection.close()
        bacalistview()
        MsgBox("Berhasil disimpan !", MsgBoxStyle.Information, "Notice !")
    End Sub
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        mycmd.Connection = myconnection.open
        mycmd.CommandText = "update tbtes set nama='" & TNAMA.Text & "' where nomor='" & TNO.Text & "'"
        mycmd.ExecuteNonQuery()
        myconnection.close()
        bacalistview()
        MsgBox("Berhasil dirubah !", MsgBoxStyle.Information, "Notice !")
    End Sub
    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        mycmd.Connection = myconnection.open
        mycmd.CommandText = "delete from tbtes where nomor='" & TNO.Text & "'"
        mycmd.ExecuteNonQuery()
        myconnection.close()
        bacalistview()
        MsgBox("Berhasil dihapus !", MsgBoxStyle.Information, "Notice !")
    End Sub
    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        bacalistview()
    End Sub
    Private Sub LISTFORM_SelectedIndexChanged(sender As Object, e As EventArgs) Handles LISTFORM.SelectedIndexChanged
        kliklistview()
    End Sub
End Class

Download Project:
Download Project
Lihat Video:

0 komentar:

Posting Komentar

Kita bukanlah apa-apa tanpaNya, jika ada pertanyaan mengenai artikel silakan tinggalkan komentar anda melalui form komentar :)

SkyArs Corp. © 2016. Tweaked by: Soni Sitez Powered by: Blogger