Hai agan-agan yang cakep-cakep...dah lama nih ane gak update postingan...mumpung lagi kepengen, jadi ane manfaatin deh untuk berbagi ilmu lewat postingan ane ini. Judulnya adalah Cara Membuat Form Login Dengan Database MySql Pada VB.Net. Sebenernya si banyak postingan di internet yang model begini, tapi jarang yang mau share dengan menggunakan database, biasanya cuma script biasa aja, login tanpa menggunakan database, jadi user dan password cuma itu-itu aja, gak bisa di create. Okelah langsung aja ya Gan..
1. Buat tabel dengan nama 'login' pada My-Sql lengkap dengan field-fieldnya, seperti pada gambar di bawah ini.
2. Pada Project VB, buat sebuah Class dengan nama "ClsAkun".
Coding nya :
Imports System.Data.Odbc
Public Class ClsAkun
Private FKdPgw As String
Private FNmPgw As String
Private FUserNm As String
Private FPassWd As String
Public Property KdPgw() As String
Get
Return FKdPgw
End Get
Set(ByVal value As String)
FKdPgw = value
End Set
End Property
Public Property NmPgw() As String
Get
Return FNmPgw
End Get
Set(ByVal value As String)
FNmPgw = value
End Set
End Property
Public Property UserNm() As String
Get
Return FUserNm
End Get
Set(ByVal value As String)
FUserNm = value
End Set
End Property
Public Property PassWd() As String
Get
Return FPassWd
End Get
Set(ByVal value As String)
FPassWd = value
End Set
End Property
Public Function Simpan() As Integer
Dim xSimpan As String
Dim X As Integer
Dim myCmd As OdbcCommand
xSimpan = "INSERT INTO login"
xSimpan &= "(Kd_Pgw,NmPgw,username,password)"
xSimpan &= "values (?,?,?,?)"
myCmd = New OdbcCommand(xSimpan, MyCn)
myCmd.Parameters.AddWithValue("Kd_Pgw", FKdPgw)
myCmd.Parameters.AddWithValue("NmPgw", FNmPgw)
myCmd.Parameters.AddWithValue("username", FUserNm)
myCmd.Parameters.AddWithValue("password", FPassWd)
myCmd.Prepare()
X = myCmd.ExecuteNonQuery
myCmd.Dispose()
Return X
End Function
Public Function Ubah() As Integer
Dim xUbah As String
Dim X As Integer
Dim xMyCmd As OdbcCommand
xUbah = "UPDATE login SET NmPgw=?,username=?,password=?"
xUbah &= "WHERE Kd_Pgw=?"
xMyCmd = New OdbcCommand(xUbah, MyCn)
xMyCmd.Parameters.AddWithValue("NmPgw", FNmPgw)
xMyCmd.Parameters.AddWithValue("username", FUserNm)
xMyCmd.Parameters.AddWithValue("password", FPassWd)
xMyCmd.Parameters.AddWithValue("Kd_Pgw", FKdPgw)
xMyCmd.Prepare()
X = xMyCmd.ExecuteNonQuery
xMyCmd.Dispose()
Return X
End Function
Public Function Hapus() As Integer
Dim xHapus As String
Dim X As Integer
Dim xMyCmd As OdbcCommand
xHapus = "DELETE FROM login WHERE Kd_Pgw=?"
xMyCmd = New OdbcCommand(xHapus, MyCn)
xMyCmd.Parameters.AddWithValue("Kd_Pgw", FKdPgw)
xMyCmd.Prepare()
X = xMyCmd.ExecuteNonQuery
xMyCmd.Dispose()
Return X
End Function
Public Function Cari() As Boolean
Dim Query As String
Dim xMyRead As OdbcDataReader
Dim xMyCmd As OdbcCommand
Query = "SELECT * FROM login WHERE Kd_Pgw=?"
xMyCmd = New OdbcCommand(Query, MyCn)
xMyCmd.Parameters.AddWithValue("Kd_Pgw", FKdPgw)
xMyRead = xMyCmd.ExecuteReader
If xMyRead.HasRows Then
xMyRead.Read()
FKdPgw = xMyRead.Item("Kd_Pgw").ToString
FNmPgw = xMyRead.Item("NmPgw").ToString
FUserNm = xMyRead.Item("username").ToString
FPassWd = xMyRead.Item("password").ToString
xMyRead.Close()
Return True
Else
xMyRead.Close()
Return False
End If
End Function
End Class
3. Buat Form Akun Pada Project VB untuk membuat Akun Login (Beri Nama Frm_Akun).
Coding nya :
Public Class Frm_Akun
Sub isiDataKeCombo()
Dim objClsPgw As New ClsPegawai
Dim oList As List(Of ClsPegawai)
oList = objClsPgw.DatakeList1("")
CmbKdPgw.Items.Clear()
For i As Integer = 0 To oList.Count - 1
CmbKdPgw.Items.Add(oList.Item(i).NIP)
Next
End Sub
Private Sub KunciObject()
CmbKdPgw.Enabled = False
TxtNmPgw.Enabled = False
TxtUser.Enabled = False
TxtPass.Enabled = False
btnSave.Enabled = False
btnUbah.Enabled = False
BtnHapus.Enabled = False
End Sub
Private Sub BukaObject()
CmbKdPgw.Enabled = True
TxtNmPgw.Enabled = True
TxtUser.Enabled = True
TxtPass.Enabled = True
btnSave.Enabled = True
btnUbah.Enabled = True
btnHapus.Enabled = True
End Sub
Private Sub BersihObject()
CmbKdPgw.Text = ""
TxtNmPgw.Text = ""
TxtUser.Text = ""
TxtPass.Text = ""
End Sub
Private Sub Frm_Akun_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim ObjClsAkun As New ClsAkun
bukaConn()
KunciObject()
isiDataKeCombo()
CmbKdPgw.Text = ""
TxtNmPgw.Text = ""
TxtUser.Text = ""
TxtPass.Text = ""
CmbKdPgw.Enabled = True
CmbKdPgw.Focus()
End Sub
Private Sub CmbKdPgw_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles CmbKdPgw.KeyPress
If Asc(e.KeyChar) = 13 Then
Dim ObjClsAkun As New ClsAkun
ObjClsAkun.KdPgw = CmbKdPgw.Text
BukaObject()
CmbKdPgw.Enabled = False
TxtUser.Focus()
If ObjClsAkun.Cari = True Then
TxtNmPgw.Text = ObjClsAkun.NmPgw
TxtUser.Text = ObjClsAkun.UserNm
TxtPass.Text = ObjClsAkun.PassWd
BtnSave.Enabled = False
Else
BtnUbah.Enabled = False
BtnHapus.Enabled = False
End If
CmbKdPgw.Focus()
End If
End Sub
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSave.Click
Dim ObjClsAkun As New ClsAkun
Dim nilaiKembali As Integer
ObjClsAkun.KdPgw = CmbKdPgw.Text
ObjClsAkun.NmPgw = TxtNmPgw.Text
ObjClsAkun.UserNm = TxtUser.Text
ObjClsAkun.PassWd = TxtPass.Text
nilaiKembali = ObjClsAkun.Simpan()
If nilaiKembali = 1 Then
MsgBox("Data Berhasil Disimpan", MsgBoxStyle.Information, "Pesan")
Else
MsgBox("Data Gagal Disimpan..Sudah Pernah Buat Username & Password Sebelumnya..!!", MsgBoxStyle.Information, "Pesan")
Me.Focus()
End If
BersihObject()
KunciObject()
CmbKdPgw.Enabled = True
CmbKdPgw.Focus()
End Sub
Private Sub btnUbah_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnUbah.Click
Dim ObjClsAkun As New ClsAkun
Dim nilaiKembali As Integer
ObjClsAkun.KdPgw = CmbKdPgw.Text
ObjClsAkun.NmPgw = TxtNmPgw.Text
ObjClsAkun.UserNm = TxtUser.Text
ObjClsAkun.PassWd = TxtPass.Text
nilaiKembali = ObjClsAkun.Ubah()
If nilaiKembali = 1 Then
MsgBox("Data Berhasil Diubah", MsgBoxStyle.Information, "Pesan")
Else
MsgBox("Data Gagal Diubah", MsgBoxStyle.Information, "Pesan")
End If
BersihObject()
KunciObject()
CmbKdPgw.Enabled = True
CmbKdPgw.Focus()
End Sub
Private Sub btnHapus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnHapus.Click
Dim ObjClsAkun As New ClsAkun
Dim nilaiKembali As Integer
ObjClsAkun.KdPgw = CmbKdPgw.Text
nilaiKembali = ObjClsAkun.Hapus()
If nilaiKembali = 1 Then
MsgBox("Data Berhasil Dihapus", MsgBoxStyle.Information, "Pesan")
End If
KunciObject()
BersihObject()
CmbKdPgw.Enabled = True
CmbKdPgw.Focus()
End Sub
Private Sub btnKeluar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnKeluar.Click
Me.Close()
End Sub
Private Sub CmbKdPgw_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmbKdPgw.SelectedIndexChanged
Dim objClsPegawai As New ClsPegawai
objClsPegawai.NIP = CmbKdPgw.Text
If objClsPegawai.Cari Then
TxtNmPgw.Text = objClsPegawai.NmPgw
End If
End Sub
End Class
4. Buat Form Login Pada Project VB untuk membuat Izin Akses Login (Beri Nama Frm_Login).
Coding nya :
Imports System.Data.Odbc
Public Class Frm_Login
Private Sub Frm_Login_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TxtUser.Text = ""
TxtPass.Text = ""
TxtUser.Focus()
End Sub
Private Sub BtnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnLogin.Click
Dim Query As String
Dim xMyRead As OdbcDataReader
Dim xMyCmd As OdbcCommand
bukaConn()
Query = "select *from login where username='" & TxtUser.Text & "' and password='" & TxtPass.Text & "'"
xMyCmd = New OdbcCommand(Query, MyCn)
xMyRead = xMyCmd.ExecuteReader
If TxtUser.Text = "" Or TxtPass.Text = "" Then
MsgBox("Opps..Data yang Anda masukan belum lengkap", MsgBoxStyle.Information, "Pesan")
TxtPass.Text = ""
TxtUser.Focus()
Exit Sub
Else
Call bukaConn()
If xMyRead.HasRows Then
xMyRead.Read()
Me.Visible = True
MenuUtama.Show()
TxtPass.Text = ""
TxtUser.Text = ""
TxtUser.Focus()
Else
MsgBox("Opps..Username & Password tidak cocok", MsgBoxStyle.Information, "Pesan")
TxtPass.Text = ""
TxtUser.Focus()
End If
End If
End Sub
Private Sub BtnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnExit.Click
Me.Close()
End Sub
End Class
Catatan Penting :
1. Pada script coding diatas Anda akan menemukan kata 'bukaConn' (text warna merah), itu fungsinya untuk memanggil koneksi pada sebuah Moduk Koneksi yang saya buat.
Ini Codingan Modul Koneksi yang saya buat di Project VB saya..
Buat sebuah Modul Koneksi Pada Project VB dengan nama "koneksi".
Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.Odbc
Module koneksi
Public StrConn As String
Public MyCn As OdbcConnection
Sub bukaConn() ' ----> Koneksi yang di panggil.
StrConn = "Dsn=2012_uas_vb4"
'StrConn &= " database=????;"
'StrConn &= "Server=localhost;"
'StrConn &= "Uid=root;pwd=;"
MyCn = New OdbcConnection(StrConn)
If MyCn.State = ConnectionState.Closed Then
Try
MyCn.Open()
Catch ex As Exception
MsgBox(ex.Message)
Application.Exit()
End Try
End If
End Sub
End Module
2. Pada Frm_Akun, ada Sebuah Combo ( Combo Pegawai ), untuk memastikan program Login ini berjalan, sebelumnya Anda harus sudah punya data Pegawai / program input, hapus, ubah data pegawai.
Ok gan, mudah-mudahan postingan ini bisa bermanfaat yaa buat agan-agan, khususnya yang memang membutuhkan banget postingan seperti ini. Kalo ada yang mau di tanyain / diskusiin silahkan gan comment, insyaAllah akan saya jawab sebisa dan setau saya.
Salam Coding Gaann...haaa