Below is my login code, i use vb.net and access database to develop my program.
Application Login
Imports System.Data.OleDb
Public Class ApplicatioLogin
Public Property UserName As String
Public Property UserPassword As String
Public Property Database As String
Public Property DatabasePassword As String
Public Property Retries As Integer
Public Sub New()
End Sub
Private Builder As New OleDb.OleDbConnectionStringBuilder With
{
.Provider = "Microsoft.ACE.OLEDB.12.0"
}
Public Function Login() As Boolean
Builder.DataSource = Me.Database
If Not String.IsNullOrWhiteSpace(Me.UserName) AndAlso Not String.IsNullOrWhiteSpace(Me.UserPassword) Then
Using cn As New OleDb.OleDbConnection With {.ConnectionString = Builder.ConnectionString}
Using cmd As New OleDb.OleDbCommand With
{
.Connection = cn,
.CommandText =
"SELECT UserName,UserPassword FROM tbl_user " &
"WHERE UserName = @UserName AND UserPassword = @UserPassword"
}
cmd.Parameters.AddWithValue("@UserName", Me.UserName)
cmd.Parameters.AddWithValue("@UserPassword", Me.UserPassword)
Try
cn.Open()
Catch ex As Exception
If ex.Message.ToLower.Contains("not a valid password") Then
Return False
Else
Throw ex
End If
End Try
Dim Reader = cmd.ExecuteScalar
If Reader IsNot Nothing Then
Retries = 0
Return True
Else
Retries += 1
Return False
End If
End Using
End Using
Else
Return False
End If
End Function
End Class
Below is my player code
when user login success (LogOnsound)
when user logout (LogOffsound)
Dim path = System.Windows.Forms.Application.StartupPath
Dim LogOnsound As String
Dim LogOffsound As String
Dim MyPlayer As New SoundPlayer()
Dim FailCount As Integer = 0
Private Sub LoginForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.CenterToScreen()
LogOnsound = "\LogOn.wav"
LogOffsound = "\LogOn.wav"
End Sub
Form 1 button login
Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click
If un.Text = String.Empty And pw.Text = String.Empty Then
MessageBox.Show("Please fill in both username and password")
Return
End If
If FailCount = 2 Then
MessageBox.Show("Sorry, you have exceeded the maximum number of attempts
to login", "Message")
Application.ExitThread()
End If
Dim AppLogin As New ApplicatioLogin With
{
.Database = IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory,"mydataX.mdb"),
.UserName = un.Text,
.UserPassword = pw.Text
}
If AppLogin.Login Then
FailCount = 0
MyPlayer.SoundLocation = path & LogOnsound
MyPlayer.Play()
Me.Hide()
un.Clear()
pw.Clear()
Else
FailCount += 1
MyPlayer.SoundLocation = path & LogOffsound
MyPlayer.Play()
MessageBox.Show("FailCount" & FailCount)
If FailCount = 3 Then
Application.Exit()
End If
un.Clear()
pw.Clear()
End If
End Sub