Friday, January 11, 2013

Contoh membuat form login dengan Visual Studio 2008

Form login yang akan di buat akan di koneksikan dengan database menggunakan mysql.
Ini adalah tabel pengguna pada database kampus :
Klik gambar untuk memperbesar.



Form Login



Imports MySql.Data.MySqlClient
Public Class Login
    Dim db As New MySql.Data.MySqlClient.MySqlConnection
    Dim rs As MySqlDataReader
    Dim cmd As MySqlCommand
    Dim sql As String

    Sub opendb()
        sql = "server=localhost;uid=root;pwd=;database=Kampus"
        Try
            db.ConnectionString = sql
            db.Open()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub

    Private Sub Ok_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Ok.Click
        Dim ketemu As String = ""
        If Username.Text = "" Or Password.Text = "" Then
            MsgBox(" Username Atau Password Tidak Boleh Kosong")
        Else
            sql = "select*from Pengguna"
            cmd = New MySqlCommand(sql, db)
            rs = cmd.ExecuteReader
            Try
                While rs.Read

                    If Username.Text = rs("xuser") And Password.Text = rs("xpass") Then
                        ketemu = "Ya"
                        Exit While
                    End If
                End While

                If ketemu = "Ya" Then
                    Menu_Utama.Show()
                    Me.Hide()
                Else
                    MsgBox("Username Dan Password Tidak Sesuai")
                End If

            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
            rs.Close()
        End If
        Username.Text = ""
        Password.Text = ""
    End Sub

    Private Sub Kembali_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Kembali.Click
        Me.Close()
    End Sub

    Private Sub Username_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Username.KeyPress

        If Asc(e.KeyChar) = 13 Then
            Password.Focus()
        End If

    End Sub

    Private Sub Password_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Password.KeyPress
        If Asc(e.KeyChar) = 13 Then
            Ok_Click(sender, e)
        End If
    End Sub

    Private Sub Login_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        opendb()
    End Sub
End Class
Jangan lupa untuk menambah satu form dengan nama Menu_Utama.
Semoga Bermanfaat

No comments:

Post a Comment