segunda-feira, 16 de setembro de 2013

on




Tela de Login
Imports System.Data
Imports System.Data.OleDb

Public Class frmLogin

    Public Function validaCampos() As Boolean
        If txtLogin.Text = "" Then
            MsgBox("Preencha o campo login.", MsgBoxStyle.Critical)
            txtLogin.Focus()
            Return False
        End If
        If txtSenha.Text = "" Then
            MsgBox("Preencha o campo senha.", MsgBoxStyle.Critical)
            txtSenha.Focus()
            Return False
        End If
        Return True
    End Function

    Private Sub PAcessarSistema()
        Dim dr As OleDbDataReader = Nothing
        Using con As OleDbConnection = GetConnection()
            Try
                con.Open()
                Dim sql As String = "SELECT * FROM usuarios WHERE login=? and senha=?"
                Dim cmd As OleDbCommand = New OleDbCommand(sql, con)
                cmd.Parameters.Add(New OleDb.OleDbParameter("@login", txtLogin.Text))
                cmd.Parameters.Add(New OleDb.OleDbParameter("@senha", txtSenha.Text))

                dr = cmd.ExecuteReader(CommandBehavior.SingleRow)

                If dr.HasRows Then
                    dr.Read()
                    strLogin = dr.Item("Login")
                    strPerfil = dr.Item("perfil")

                    Me.Dispose()
                    frmPrincipal.Show()
                Else
                    MsgBox("Dados inválidos. Por favor tente novamente.", MsgBoxStyle.Critical)
                    txtLogin.Text = ""
                    txtSenha.Text = ""
                    txtLogin.Focus()
                End If
            Catch ex As Exception              
            Finally
                dr.Close()
                con.Close()
            End Try
        End Using
    End Sub

    Private Sub btnLogar_Click(sender As Object, e As EventArgs) Handles btnLogar.Click
        If validaCampos() = False Then Exit Sub
        PAcessarSistema()
    End Sub

    Private Sub btnCancelar_Click(sender As Object, e As EventArgs) Handles btnCancelar.Click
        intPergunta = MsgBox("Tem certeza que deseja encerrar a aplicação?", MsgBoxStyle.Question + MsgBoxStyle.YesNo)
        If intPergunta <> vbYes Then Exit Sub
        End
    End Sub
End Class

Tela Principal
Public Class frmPrincipal

    Private Sub frmPrincipal_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        lblLogin.Text = strLogin
        lblPerfil.Text = strPerfil
    End Sub
End Class

Código do Módulo
Imports System.Data
Imports System.Data.OleDb

Module mdlAcesso

    Public strLogin As String
    Public strPerfil As String
    Public intPergunta As Integer

    Public Function GetConnection() As OleDbConnection
        Dim strConnection As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\Projetos Visual Studio 2012\SistemaLogin\sistema.accdb"
        Return New OleDbConnection(strConnection)
    End Function

End Module


Espero ter ajudado, abraços.

0 comentários:

Postar um comentário