Imports System.Text
Imports System.Data.OleDb
Public Class LoginForm1
Dim LogCount As Integer = 0
Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
If CheckUser(UsernameTextBox.Text, PasswordTextBox.Text) = True Then
If UserStatus = "0" Then 'ถ้าไม่ใช่ผู้ดูแลระบบ
'code to do something ... (กำหนดให้บางเมนูที่หน้าต่างหลักใช้ไม่ได้)
End If
frmMain.Text = "ชื่อระบบ : Login by ---> " & UserFullName
frmMain.Show()
Me.Hide()
Else
LogCount = LogCount + 1
If LogCount = 3 Then
MessageBox.Show("ล๊อกอินเป็นครั้งที่ 3 ไม่สำเร็จ ระบบจะปิดการทำงาน!...", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Application.Exit()
End If
MessageBox.Show("ชื่อผู้ใช้หรือรหัสผ่านไม่ถูกต้อง!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
UsernameTextBox.Clear() : UsernameTextBox.Focus()
PasswordTextBox.Clear()
End If
End Sub
Private Function CheckUser(ByVal _UserName As String, ByVal _Password As String) As Boolean
LinkDB()
sb = New StringBuilder
Dim UserName As New OleDbParameter("UserName", OleDbType.VarChar)
Dim Password As New OleDbParameter("Password", OleDbType.VarChar)
sb.Append("select * from users where ")
sb.Append("(username=?) and (password=?) and isnormal='1'")
'sb.Append(" and isnormal='1'")
strCar = sb.ToString()
com = New OleDbCommand
With com
.CommandType = CommandType.Text
.CommandText = strCar
.Connection = Conn
.Parameters.Add(UserName).Value = UsernameTextBox.Text
.Parameters.Add(Password).Value = PasswordTextBox.Text
dr = .ExecuteReader
End With
If dr.HasRows Then
dr.Read()
UserStatus = dr.Item("status").ToString()
UserFullName = dr.Item("fullname").ToString()
dr.Close()
Return True
Else
dr.Close()
Return False
End If
Conn.Close()
End Function
Private Sub UsernameTextBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles UsernameTextBox.KeyPress
If e.KeyChar = Chr(13) Then
PasswordTextBox.Focus()
End If
End Sub
Private Sub PasswordTextBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles PasswordTextBox.KeyPress
If e.KeyChar = Chr(13) Then
OK.PerformClick()
End If
End Sub
End Class