Membuat class Data Access Object di VB.NET

Implementasi Object Oriented Programming di VB.NET akan membuat kode program VB.NET anda lebih mudah dalam proses maintenance kode program. Pada tutorial kali ini, kiita akan membuat class Induk dari Data Access Object DAOMaster.vb yang akan digunakan pada sub class Data Access Object pada implementasi di table database anda.

Pada tutorial ini penulis menggunakan database SQL Server dengan API VB.NET class-class System.Data.SqlClient

  1. Class Master Data Access Object :: DAOMaster.vb
    Imports System.Data.SqlClient
    Public Class DAOMaster
    
        Private dbkon As DBKoneksi
        'variable nama_table akan di set oleh sub class
        Protected nama_table As String
        Public Sub New()
            'inisialisasi koneksi database ..
            dbkon = New DBKoneksi()
    
        End Sub
    
        Public Function koneksi() As DBKoneksi
            Return dbkon
        End Function
    
        Public Sub fillGrid(ByRef dg As DataGridView, _
                            ByRef sql As String)
            Dim cmd As SqlCommand
            Dim da As SqlDataAdapter
            Dim ds As DataSet
            cmd = New SqlCommand(sql, koneksi().openKoneksi())
            da = New SqlDataAdapter(cmd)
            ds = New DataSet()
            da.Fill(ds, "tbl")
            dg.DataSource = ds.Tables("tbl")
        End Sub
    
        Public Sub exeSQL(ByRef sql As String)
    
            Dim cmd As SqlCommand
            cmd = New SqlCommand(sql, koneksi().openKoneksi())
            cmd.ExecuteNonQuery()
            koneksi().closeKoneksi()
    
        End Sub
    End Class
    

    [/li]

  2. Berikut ini sub class dari DAOMaster yang bertanggung jawab terhadap tabel pegawai pada database
    nama classnya DAOPegawai

    Imports System.Data.SqlClient
    Public Class DAOPegawai
        Inherits DAOMaster
    
        Private dbkon As DBKoneksi
        Public Sub New()
            MyBase.New()
            nama_table = "pegawai"
        End Sub
    
       Public Function create() As Pegawai
            Return New Pegawai()
        End Function
    
       Public Sub simpan(ByRef obj As Pegawai)
            Dim sql As String
            sql = "INSERT INTO pegawai (nama,telpon) " & _
                  " VALUES ('" & obj.nama & "'," & _
                  "'" & obj.telpon & "')"
            exeSQL(sql)
        End Sub
    
       Public Sub delete(ByRef obj As Pegawai)
            Dim sql As String
            sql = "DELETE FROM pegawai WHERE id=" & obj.id
            exeSQL(sql)
        End Sub
    
       Public Sub isiGrid(ByRef dg As DataGridView)
            Dim sql As String
            sql = "SELECT * FROM pegawai ORDER BY a.nama"
            fillGrid(dg, sql)
        End Sub
    
    End Class
    
  3. Berikut contoh kode program pada form yang didalamnya terdapat komponen DataGridView

    Public Class FormPegawai
        Dim dao As DAOPegawai
        Private Sub FormPegawai_Load(ByVal sender As System.Object, _
                             ByVal e As System.EventArgs) Handles MyBase.Load
            dao = New DAOPegawai()
            dao.isiGrid(DataGridView1)
        End Sub
    
        Private Sub btnSimpan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSimpan.Click
    
            dim obj as Pegawai
            obj = new Pegawai()
            'set id pegawai = 1 akan dihapus ... test saja
            obj.id=1
    
            dao.delete(obj)
    
         End sub
    End Class
    

Post a Response