Option Explicit On
Option Strict On
Imports System.Text
Imports System.Data
Imports System.Data.SqlClient
Public Class fromLogin
Dim Conn As SqlConnection
Dim com As SqlCommand
Dim dr As SqlDataReader
Dim tr As SqlTransaction
Dim sb As StringBuilder
Public Shared str As String
Dim status As String
Private Sub fromLogin_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim strConn As String
strConn = DBConnString.strConn
Conn = New SqlConnection()
With Conn
If .State = ConnectionState.Open Then .Close()
.ConnectionString = strConn
.Open()
End With
txtUserName.Focus()
End Sub
Private Sub Login()
sb = New StringBuilder()
sb.Append("SELECT User_Name,User_Type FROM tb_User")
sb.Append(" WHERE User_Name=@User_Name")
Dim sqlLogin As String
sqlLogin = sb.ToString()
com = New SqlCommand()
With com
.CommandText = sqlLogin
.CommandType = CommandType.Text
.Connection = Conn
.Parameters.Clear()
.Parameters.Add("@User_Name", SqlDbType.VarChar).Value = txtUserName.Text
dr = .ExecuteReader()
If dr.HasRows Then
With dr
.Read()
status = .GetString(.GetOrdinal("User_Type"))
End With
End If
End With
dr.Close()
'***************************************************************************************************
If status = "พนักงาน" Then
sb = New StringBuilder()
sb.Append("SELECT User_Name,User_Password FROM tb_User")
sb.Append(" WHERE (User_Name=@User_Name)")
sb.Append(" AND (User_Password=@User_Password)")
sqlLogin = sb.ToString()
com = New SqlCommand()
With com
.CommandText = sqlLogin
.CommandType = CommandType.Text
.Connection = Conn
.Parameters.Clear()
.Parameters.Add("@User_Name", SqlDbType.VarChar).Value = txtUserName.Text
.Parameters.Add("@User_Password", SqlDbType.VarChar).Value = txtPassword.Text
dr = .ExecuteReader()
If dr.HasRows Then
str = "0"
fromMainn.Show()
Me.Hide()
Else
MessageBox.Show("UserName หรือ Password ที่คุณป้อน ไม่ถูกต้อง !!!", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End If
End With
dr.Close()
ElseIf status = "เจ้าของร้าน" Then
sb.Remove(0, sb.Length())
sb.Append("SELECT User_Name,User_Password FROM tb_User")
sb.Append(" WHERE (User_Name=@User_Name)")
sb.Append(" AND (User_Password=@User_Password)")
sqlLogin = sb.ToString()
com = New SqlCommand()
With com
.CommandText = sqlLogin
.CommandType = CommandType.Text
.Connection = Conn
.Parameters.Clear()
.Parameters.Add("@User_Name", SqlDbType.VarChar).Value = txtUserName.Text
.Parameters.Add("@User_Password", SqlDbType.VarChar).Value = txtPassword.Text
dr = .ExecuteReader()
If dr.HasRows Then
str = "1"
fromMainn.Show()
Me.Hide()
Else
MessageBox.Show("UserName หรือ Password ที่คุณป้อน ไม่ถูกต้อง !!!", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End If
End With
dr.Close()
End If
End Sub
Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click
Login()
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
Application.Exit()
Me.Close()
End Sub
End Class
Option Explicit On
Option Strict On
Public NotInheritable Class DBConnString
Public Shared strConn As String = "Data Source=.\SQLExpress;Initial Catalog=Bakery;Integrated Security=True"
'Public Shared strConn As String = "Data Source=HNONG\SQLExpress;Initial Catalog=Deer;User ID=sa1;Password=deer1234"
'Data Source=.\SqlExpress;AttachDbFilename=" & strDBpath & ";Integrated Security=True;User Instance=True"
Public Shared UserName As String = ""
Public Shared UserAuthentication As String = ""
End Class