Assalamualaikum, ok sekarang saya akan memberikan cara membuat Login di VB.NET menggunakan database MySql, sebelum ke kodingnya kalian harus membuat koneksi terlebih dahulu jika belum membuat koneksi nya buat dulu koneksi database ke MySql disini, ok lanjut saja ke cara yang pertama
- Tambahkan dua variable public dimodule
- MYSQLCMD As MySqlCommand
- MYSQLDR As MySqlDataReader
Imports MySql.Data.MySqlClient 'ini harus ada di atas !
Module SQLControl
Dim SERVER As String = "Localhost"
Dim PORT As String = "3306"
Dim UID As String = "root"
Dim PASSWORD As String = ""
Dim DATABASE As String = "db_latihan"
Dim ConnectionString As String = "SERVER=" & SERVER & ";PORT=" & PORT & ";UID=" & UID & ";PASSWORD=" & PASSWORD & ";DATABASE=" & DATABASE & ""
Public SqlCon As New MySqlConnection(ConnectionString)
Public MYSQLCMD As MySqlCommand
Public MYSQLDR As MySqlDataReader
End Module
Module SQLControl
Dim SERVER As String = "Localhost"
Dim PORT As String = "3306"
Dim UID As String = "root"
Dim PASSWORD As String = ""
Dim DATABASE As String = "db_latihan"
Dim ConnectionString As String = "SERVER=" & SERVER & ";PORT=" & PORT & ";UID=" & UID & ";PASSWORD=" & PASSWORD & ";DATABASE=" & DATABASE & ""
Public SqlCon As New MySqlConnection(ConnectionString)
Public MYSQLCMD As MySqlCommand
Public MYSQLDR As MySqlDataReader
End Module
- Setelah itu buat sebuah Sub Reader di Module
Sub Reader(query As String)
Try
MYSQLCMD = New MySqlCommand(query, SqlCon)
MYSQLDR = MYSQLCMD.ExecuteReader
MYSQLDR.Read()
Catch ex As Exception
MsgBox("Gagal, Karena " & ex.Message, MsgBoxStyle.Exclamation, "Gagal")
End Try
End Sub
Try
MYSQLCMD = New MySqlCommand(query, SqlCon)
MYSQLDR = MYSQLCMD.ExecuteReader
MYSQLDR.Read()
Catch ex As Exception
MsgBox("Gagal, Karena " & ex.Message, MsgBoxStyle.Exclamation, "Gagal")
End Try
End Sub
- Setelah itu buat Function baru di Module untuk mengatasi karakter yg bisa menyebabkan error query MySql, contohnya, ' \ ` "
If Trim(UsernameTextBox.Text) = "" Then
MsgBox("Username Tidak boleh Kosong !", MsgBoxStyle.Information, "Login")
UsernameTextBox.Select()
ElseIf Trim(PasswordTextBox.Text) = "" Then
MsgBox("Password Tidak boleh Kosong !", MsgBoxStyle.Information, "Login")
PasswordTextBox.Select()
Else
Try
SqlCon.Open()
Reader("SELECT * FROM t_user WHERE username = '" & EscapeString(UsernameTextBox.Text) & "'")
If MYSQLDR.HasRows = True Then
If MYSQLDR.Item(2) = PasswordTextBox.Text Then
' = = = koding jika login berhasil = = = '
' terserah mau diisi apa oleh kalian ! '
Else
MsgBox("Password Salaha !", MsgBoxStyle.Exclamation, "Salah !")
PasswordTextBox.Select()
PasswordTextBox.SelectAll()
End If
Else
MsgBox("Username Tidak ditemukan !", MsgBoxStyle.Information, "Login")
UsernameTextBox.Select()
UsernameTextBox.SelectAll()
End If
SqlCon.Close()
Catch ex As Exception
MsgBox("Gagal, Karena " & ex.Message, MsgBoxStyle.Exclamation, "Gagal !")
If SqlCon.State = ConnectionState.Open Then
SqlCon.Close()
End If
End Try
End If
MsgBox("Username Tidak boleh Kosong !", MsgBoxStyle.Information, "Login")
UsernameTextBox.Select()
ElseIf Trim(PasswordTextBox.Text) = "" Then
MsgBox("Password Tidak boleh Kosong !", MsgBoxStyle.Information, "Login")
PasswordTextBox.Select()
Else
Try
SqlCon.Open()
Reader("SELECT * FROM t_user WHERE username = '" & EscapeString(UsernameTextBox.Text) & "'")
If MYSQLDR.HasRows = True Then
If MYSQLDR.Item(2) = PasswordTextBox.Text Then
' = = = koding jika login berhasil = = = '
' terserah mau diisi apa oleh kalian ! '
Else
MsgBox("Password Salaha !", MsgBoxStyle.Exclamation, "Salah !")
PasswordTextBox.Select()
PasswordTextBox.SelectAll()
End If
Else
MsgBox("Username Tidak ditemukan !", MsgBoxStyle.Information, "Login")
UsernameTextBox.Select()
UsernameTextBox.SelectAll()
End If
SqlCon.Close()
Catch ex As Exception
MsgBox("Gagal, Karena " & ex.Message, MsgBoxStyle.Exclamation, "Gagal !")
If SqlCon.State = ConnectionState.Open Then
SqlCon.Close()
End If
End Try
End If
- Selesai, Tinggal dicoba
Note : yangg dimaksud item(2) itu adalah posisi Field Password yg ada di Table User berhubung Field password saya ada diposisi ke ketiga, jadi saya masukin 2, kenapa 2 enggk 3 ?, karena itu seperti array yg dimana posisi pertama bukan 1 melainkan nol ( 0 ) seperti gambar dibawah
Selamat mencoba, good luck 😄
No comments:
Write comments