Imports MySql.Data.MySqlClient
Imports System.Data
Public Class frmLogin
Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click
If Me.txtUsername.Text = "" Then
MessageBox.Show("โปรดใส่ Username!")
Me.txtUsername.Focus()
Exit Sub
End If
If Me.txtPassword.Text = "" Then
MessageBox.Show("โปรดใส่ Password!")
Me.txtPassword.Focus()
Exit Sub
End If
Label1.Text = "กำลังประมวลผลข้อมูล..."
Timer1.Enabled = True
End Sub
Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
If MessageBox.Show("คุณต้องการออกจากโปรแกรมใช่หรือไม่ ?", "ยืนยันการออกจากโปรแกรม", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) = DialogResult.OK Then
Application.Exit()
End If
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim i As Integer
If ProgressBar1.Value < 100 Then
ProgressBar1.PerformStep()
i = i + 1
If ProgressBar1.Value >= 100 Then
Timer1.Enabled = False
'MsgBox("ระบบประมวลผลเสร็จสิ้น....", MsgBoxStyle.Information, "")
Label1.Text = "ประมวลผลเสร็จสิ้น!"
ProgressBar1.Value = 0
Dim objConn As New MySqlConnection
Dim objCmd As New MySqlCommand
Dim strConnString, strSQL As String
strConnString = "Server=localhost;User Id=root; Password=0000; Database=members; Pooling=false"
objConn.ConnectionString = strConnString
objConn.Open()
Dim intNumRows As Integer
strSQL = "SELECT COUNT(*) FROM member WHERE Username = '" & Me.txtUsername.Text & "' AND Password = '" & Me.txtPassword.Text & "' "
objCmd = New MySqlCommand(strSQL, objConn)
intNumRows = objCmd.ExecuteScalar()
If intNumRows > 0 Then
Dim frm As New frmMain
frm._strUser = Me.txtUsername.Text
frm.LoadInfor()
frm.Show()
Me.Hide()
Else
MessageBox.Show("[Error] ชื่อผู้ใช้งานไม่ถูกต้อง" & vbCrLf & "[Error] รหัสผ่านไม่ถูกต้อง", "ข้อผิดพลาดของโปรแกรม", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
objConn.Close()
objConn = Nothing
End If
End If
End Sub
End Class
ใช้ DataTable แทนครับ จะสามารถดึงประเภทของ User ขึ้นมาได้ครับ
Code (VB.NET)
Dim objConn As SqlConnection
Dim strConnString As String
Dim strSQL As StringBuilder
Dim objCmd As SqlCommand
Dim dtAdapter As SqlDataAdapter
Dim dt As DataTable
Dim ds As New DataSet
'*** Open Connection ***'
strConnString = "Server=localhost;UID=sa;PASSWORD=;database=mydatabase;Max Pool Size=400;Connect Timeout=600;"
objConn = New SqlConnection
objConn.ConnectionString = strConnString
objConn.Open()
strSQL = New StringBuilder
strSQL.Append(" SELECT * FROM member ")
strSQL.Append(" WHERE Username = @sUsername ")
objCmd = New SqlCommand(strSQL.ToString(), objConn)
objCmd.Parameters.Add("@sUsername", SqlDbType.VarChar).Value = Session("strUsername")
dtAdapter = New SqlDataAdapter
dtAdapter.SelectCommand = objCmd
dtAdapter.Fill(ds)
dt = ds.Tables(0)
dtAdapter = Nothing
objConn.Close()
objConn = Nothing
If dt.Rows.Count > 0 Then
IF dt.Rows[0]["Type"] == "Admin" Then
// Admin
ElseIF dt.Rows[0]["Type"] == "User" Then
// User
End IF
End If