Option Explicit On
Option Strict On
Imports System.Data
Imports System.Data.SqlClient
Imports System.IO
Public Class frmlogin
Inherits System.Windows.Forms.Form
Dim da As New SqlDataAdapter
Dim ds As DataSet = New DataSet
Dim Invalid As Integer = 0
Dim conn As New SqlConnection
Dim strcon As String = "Data Source=lolove-e8faf31f;Initial Catalog = CD Center;Integrated Security= True; user id = sa; password= '' "
Dim tmpRowsAffected As Long = 0
Dim xmlFilePath As String = "C:\\UserlogFile.xml"
Dim IsFind As Boolean = False
' Dim strconn AS String = "Data Source=lolove-e8faf31f;Initial Catalog = CD Center;Integrated Security = True;user id = sa; password = ''"
Public tmpUserName As String = ""
Private Sub Ok_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Ok.Click
Dim conn As SqlConnection = New SqlConnection
conn.ConnectionString = strcon
If (TxtUsername.Text = "") Or (TxtPassword.Text = "") Then
MessageBox.Show("กรุณาป้อนข้อมูลให้ครบ !!!", "ผลการตรวจสอบ", _
MessageBoxButtons.OK, MessageBoxIcon.Warning)
TxtUsername.Focus()
Exit Sub
End If
Dim sqlUser As String = ""
sqlUser = "SELECT * From UserName"
sqlUser &= " where (username ='" & TxtUsername.Text & "')"
sqlUser &= " AND (password='" & TxtPassword.Text & " ')"
sqlUser &= " AND (IsNormal='1')"
With conn
If .State = ConnectionState.Open Then .Close()
' .ConnectionString = StrConn
.Open()
End With
If IsFind = True Then
ds.Tables("UserName").Clear()
End If
da = New SqlDataAdapter(sqlUser, conn)
da.Fill(ds, "UserName")
If ds.Tables("UserName").Rows.Count <> 0 Then
IsFind = True
Invalid = 0
Dim fmain As New frmmain
fmain.Show()
Me.Hide()
tmpUserName = TxtUsername.Text
Else
IsFind = False
WriteLogFile()
If Invalid = 3 Then
UpdateInvalidUser()
If tmpRowsAffected <> 0 Then
MessageBox.Show("คุณป้อนรหัส Password ผิด ชื่อ UserName:" & _
TxtUsername.Text & "ไม่สามารถใช้งานได้ชั่วคราว กรุณาติดต่อผู้ดูแลระบบ !!! ", _
"ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Warning)
TxtUsername.Text = ""
TxtUsername.Focus()
TxtPassword.Text = ""
Invalid = 0
Exit Sub
End If
End If
MessageBox.Show("ข้อมูลผู้ใช้งานไม่ถูกต้อง ไม่สามารถเข้าระบบได้ !!!", "ผลการตรวจสอบ", _
MessageBoxButtons.OK, MessageBoxIcon.Information)
TxtPassword.Text = ""
TxtPassword.Focus()
End If
End Sub
Private Sub WriteLogFile()
Dim today As Date
Dim dsxml As DataSet
Try
dsxml.ReadXml(xmlFilePath, XmlReadMode.Auto)
Dim dr As DataRow
dr = dsxml.Tables(0).NewRow
dr("Username") = TxtUsername.Text
dr("Password") = TxtPassword.Text
dr("DataTimeToLogin") = today
dsxml.Tables(0).Rows.Add(dr)
dsxml.WriteXml(xmlFilePath, XmlWriteMode.IgnoreSchema)
Catch
Dim strFirstContent As String = ""
strFirstContent = "<UserLog>" & vbCrLf
strFirstContent &= "<Username>" & TxtUsername.Text & "</Username>" & vbCrLf
strFirstContent &= "<Password>" & TxtPassword.Text & "</Password>" & vbCrLf
strFirstContent &= "<Datetimelogin>" & today & "</Datetimelogin>" & vbCrLf
strFirstContent &= "</UserLog>"
Dim sw As StreamWriter = New StreamWriter(xmlFilePath)
sw.Write(strFirstContent)
sw.Flush()
sw.Close()
End Try
Invalid = Invalid + 1
End Sub
Private Sub UpdateInvalidUser()
Dim conn As SqlConnection = New SqlConnection
Dim SqlUpdate As String = ""
SqlUpdate = "Update Username"
SqlUpdate &= " set IsNormal = '0'"
SqlUpdate &= "where (username=' " & TxtUsername.Text & " ')"
With conn
If .State = ConnectionState.Open Then .Close()
.ConnectionString = strcon
.Open()
End With
Dim tr As SqlTransaction
Dim comUpdate As SqlCommand
tr = conn.BeginTransaction
comUpdate = New SqlCommand
With comUpdate
.CommandType = CommandType.Text
.CommandText = SqlUpdate
.Connection = conn
.Transaction = tr
tmpRowsAffected = .ExecuteNonQuery
End With
Invalid = 0
tr.Commit()
End Sub
Private Sub FrmLogin_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim conn As SqlConnection = New SqlConnection
Dim strconn As String = "Data Source=lolove-e8faf31f;Initial catalog=CD Center;Integrated Security = True"
Cursor.Current = Cursors.WaitCursor
With conn
If .State = ConnectionState.Open Then .Close()
.ConnectionString = strcon
.Open()
End With
Cursor.Current = Cursors.Default
End Sub
Private Sub Ext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Ext.Click
If MessageBox.Show("คุณต้องการจบการทำงานใช่หรือไม่", "คำยืนยัน", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
End
End If
End Sub
End Class