Sumber Gambar |
Bingung nih ane mau nulis apa di blog ini, udah lama juga gak nulis di blog ane. Dari pada kelamaan bingungnya mending ane informasiin aja beberapa class koneksi yang pernah ane dapet dan ane karang atau ane modif-modif dikit.
Ok gan langsung aja ke TKP gan
Versi pertama :
Imports Microsoft.VisualBasic
Imports MySql.Data.MySqlClient
Public Class ClsKoneksi
Protected tblUser = New DataTable
Protected SQL As String
Protected Cn As MySqlConnection
Protected Cmd As MySqlCommand
Protected Da As MySqlDataAdapter
Protected Ds As DataSet
Protected Dt As DataTable
Public Function OpenConn() As Boolean
Cn = New MySqlConnection("server=localhost; user id=root; password='root'; database=dbpenjualan")
Cn.Open()
If Cn.State <> ConnectionState.Open Then
Return False
Else
Return True
End If
End Function
Public Sub CloseConn()
If Not IsNothing(Cn) Then
Cn.Close()
Cn = Nothing
End If
End Sub
Public Function ExecuteQuery(ByVal Query As String) As DataTable
If Not OpenConn() Then
MsgBox("Koneksi Gagal..!!", MsgBoxStyle.Critical, "Access Failed")
Return Nothing
Exit Function
End If
Cmd = New MySqlCommand(Query, Cn)
'Cmd = New OleDb.OleDbCommand(Query, Cn)
'Da = New OleDb.OleDbDataAdapter
Da = New MySqlDataAdapter
Da.SelectCommand = Cmd
Ds = New Data.DataSet
Da.Fill(Ds)
Dt = Ds.Tables(0)
Return Dt
Dt = Nothing
Ds = Nothing
Da = Nothing
Cmd = Nothing
CloseConn()
End Function
Public Sub ExecuteNonQuery(ByVal Query As String)
If Not OpenConn() Then
MsgBox("Koneksi Gagal..!!", MsgBoxStyle.Critical, "Access Failed..!!")
Exit Sub
End If
Cmd = New MySqlCommand(Query, Cn)
Cmd.Connection = Cn
Cmd.CommandType = CommandType.Text
Cmd.CommandText = Query
Cmd.ExecuteNonQuery()
Cmd = Nothing
CloseConn()
End Sub
End Class
Versi kedua :
Imports MySql.Data.MySqlClient
Imports MySql.Data
Module Konek
Public Database As New MySqlConnection
Public Tampilgbr As New MySqlCommand
Public Tampilkangbr As MySqlDataReader
Public Grid As New MySqlDataAdapter
Public Ds As New DataSet
Public Tabel As String
Public record As New BindingSource
Public DMLSql As New MySqlCommand
Private con As MySqlConnection = Nothing
Public Function ok() As MySqlConnection
Dim constring As String
constring = ";server=localhost" & ";user=root" & ";password=root" & ";database=dbpenjualan"
Try
con = New MySqlConnection(constring)
con.Open()
Catch ex As Exception
MessageBox.Show(ex.Message, "Koneksi error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
Return con
End Function
Public Sub KoneksiDatabase()
Try
Database.Close()
Database.ConnectionString = "server=localhost; user id=root; password='root'; database=dbpenjualan"
Database.Open()
Catch ex As Exception
MsgBox(ex.ToString())
End Try
End Sub
End Module
Versi ketiga
Imports Microsoft.VisualBasic
Imports MySql.Data.MySqlClient
Module Module1
Public conn As MySqlConnection
Public da As MySqlDataAdapter
Public ds As DataSet
Public cmd As MySqlCommand
Public rd As MySqlDataReader
Public Str As String
Public Sub Koneksi()
Str = "server=localhost; user id=root; password='root'; database=dbpenjualan"
conn = New MySqlConnection(Str)
If conn.State = ConnectionState.Closed Then
conn.Open()
End If
End Sub
End Module
Untuk implementasinya bisa disesuaikan dengan kebutuhan agan semua tergatung dari basis database yang digunakan (Access, SQL, MySQL dll) dan juga tergantung dari desain aplikasi yang agan buat.
Untuk fersi ke tiga dan kedua menggunakan Module VB Class sedangkan untuk fersi pertama menggunakan VB Class.
Maaf kata-katanya ancur2an gini gan, semoga bermanfaat aja and sampai ketemu lagi di artikel beriktunya!...
kalau ada yang punya class connection selain yang ane tulis diatas bisa di share gan
ReplyDelete