Membuat Class Koneksi vb.net 2012 dengan MySql
Sebelumnya kita harus download dulu mysql.data conector untuk .net, saya anggap anda sudah paham dengan mysql.data hehehe. selanjutnya kita Membuat Class Koneksi database vb.net 2012 dengan MySqllangsung saja buat file class.
Imports MySql.Data.MySqlClient Public Class ClsKoneksi Protected SQL As String Protected Cn As New MySqlConnection Protected Cmd As New MySql.Data.MySqlClient.MySqlCommand Protected Da As New MySql.Data.MySqlClient.MySqlDataAdapter Protected DS As New DataSet Protected Dt As DataTable Public Function OpenConn() As Boolean On Error Resume Next Dim FilePath As String = Application.StartupPath & "\setting.ini" Cn = New MySqlConnection("server=localhost;"_ & "user id=root;" _ & "password=;" _ & "database=namadatabase") 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..!! Harap periksa pengaturan koneksi...!!", MsgBoxStyle.Critical, "Access Failed") Return Nothing Exit Function End If Cmd = New MySql.Data.MySqlClient.MySqlCommand(Query, Cn) Da = New MySql.Data.MySqlClient.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..!! Harap periksa pengaturan koneksi...!!", MsgBoxStyle.Critical, "Access Failed") Exit Sub End If Cmd = New MySql.Data.MySqlClient.MySqlCommand Cmd.Connection = Cn Cmd.CommandType = CommandType.Text Cmd.CommandText = Query Cmd.ExecuteNonQuery() Cmd = Nothing CloseConn() End Sub End Class
0 komentar:
Post a Comment
Silahkan tinggalkan komentar yang baik dan relevan terimaksih