Rabu, 08 Juni 2011

bikin login dengan navicat mysql

1. Membuat Class koneksi

Imports MySql.Data.MySqlClient

Public Class Koneksi

Private Conn As MySqlConnection = Nothing

Public Function konek() As MySqlConnection

Dim ConnString As String

ConnString = ";server=localhost;user=root;password=;database=shofwah "

Try

Conn = New MySqlConnection(ConnString)

Conn.Open()

Catch ex As Exception

MessageBox.Show("Koneksi Erros: " + ex.Message)

End

End Try

Return Conn

End Function

End Class

2. Membuat module myModul

Imports MySql.Data.MySqlClient

Module myModul

Public conn As New MySqlConnection

Public DTadapter As New MySqlDataAdapter

Public DTset As DataSet

Public DTrow As DataRow

Public CMD As MySqlCommand

Public DTreader As MySqlDataReader

Public DTtable As New DataTable

Public CMDbuild As MySqlCommandBuilder

Public sql As String

Dim currentRow As Integer

Private strkoneksi As String

End Module

3. LoginForm1

Imports MySql.Data.MySqlClient

Public Class LoginForm1

Dim conn As New Koneksi

Dim msql As String

Dim currentRow As Integer

Public Shared user, pass, akses As String

' TODO: Insert code to perform custom authentication using the provided username and password

' (See http://go.microsoft.com/fwlink/?LinkId=35339).

' The custom principal can then be attached to the current thread's principal as follows:

' My.User.CurrentPrincipal = CustomPrincipal

' where CustomPrincipal is the IPrincipal implementation used to perform authentication.

' Subsequently, My.User will return identity information encapsulated in the CustomPrincipal object

' such as the username, display name, etc.

Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

Me.Close()

End Sub

Private Sub CariData()

Try

DTset = New DataSet

sql = "select * from login where username = '" & UsernameTextBox.Text.Trim & "'"

DTadapter.SelectCommand = New MySqlCommand(sql, conn.konek)

currentRow = 0

DTadapter.Fill(DTset, "petugas")

DTtable = DTset.Tables("petugas")

conn.konek.Close()

Catch ex As Exception

MessageBox.Show(ex.Message)

End

End Try

End Sub

Private Sub LihatData()

With DTset.Tables("petugas")

user = .Rows(currentRow)("username").ToString

pass = .Rows(currentRow)("password").ToString

akses = .Rows(currentRow)("status").ToString

End With

End Sub

Private Sub Login()

CariData()

If DTtable.Rows.Count > 0 Then

LihatData()

If (PasswordTextBox.Text.Trim) = pass And akses = Cbacces.Text.Trim Then

Try

If Cbacces.Text = "admin" Then

FormAwal.Show()

Me.Visible = False

Else

If Cbacces.Text = "user" Then

FormAwal.Enabled = False

FormAwal.Show()

Me.Visible = False

Else

MessageBox.Show("Hak aksese anda tidak sesuai")

End If

End If

Catch ex As Exception

MessageBox.Show(ex.Message)

End Try

Else

MessageBox.Show("Kombinasi User,Password dan acces yang anda masukkan salah", "SIM", MessageBoxButtons.OK)

UsernameTextBox.Clear()

PasswordTextBox.Clear()

UsernameTextBox.Focus()

OK.Text = "Cencel"

End If

Else

MessageBox.Show("Username yang Anda Masukkan Tidak Terdaftar", "SIM")

UsernameTextBox.Focus()

UsernameTextBox.Clear()

PasswordTextBox.Clear()

OK.Text = "Cencel"

End If

End Sub

Sub proses()

If OK.Text = "Login" Then

Call Login()

Else

End

End If

End Sub

Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

Call proses()

End Sub

Private Sub UsernameTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UsernameTextBox.TextChanged

If UsernameTextBox.Text = "" Then

OK.Text = "Cancel"

Else

OK.Text = "Login"

End If

End Sub

Private Sub LoginForm1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Cbacces.Items.Add("user")

Cbacces.Items.Add("admin")

End Sub

Private Sub OK_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click

If OK.Text = "Login" Then

Call proses()

Else

End

End If

End Sub

End Class

Tidak ada komentar:

Posting Komentar