1. สร้างฐานข้อมูล
สร้าง Database ขึ้นมาชื่อ Login และสร้าง Table ขึ้นมา 2 Table ชื่อ Member และ User
2. สร้าง Table Member ดังนี้ Data Type กำหนดเองแล้วแต่ว่าต้องการแค่ไหน
3. สร้าง Table User ดังนี้ Data Type กำหนดเองแล้วแต่ว่าต้องการแค่ไหน
4. ใส่ข้อมูลลงใน ตารางทั้ง 2 ตารางให้ครบ
5. มาทำหน้า Login ที่ Visual Studio 2013 ตั้งชื่อ Form1
6. สร้างหน้าForm 2 ตั้งชื่อ Home
7. เขียน Code ที่ Form1
Imports System.Data.SqlClient
Imports System.Data
Public Class Form1
Dim con As SqlConnection
Dim dbreader As SqlDataReader
Dim results As String
Dim cmd As SqlCommand
Dim strSQL, strsqluser As String
Public Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
results = “Data Source=HGT02-PC;Initial Catalog=Login;Persist Security Info=True;User ID=sa;Password=123456”
con = New SqlConnection(results)
Try
con.Open()
MsgBox(“Connection Open”)
Catch ex As Exception
MsgBox(“Can not open connection”)
End Try
End Sub
Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim intNumRows As Integer
strSQL = “SELECT COUNT(*) FROM Member WHERE [username] = ‘” & Me.TextBox1.Text & “‘ AND [password] = ‘” & Me.TextBox2.Text & “‘ “
cmd = New SqlCommand(strSQL, con)
intNumRows = cmd.ExecuteScalar()
If intNumRows > 0 Then
‘ตรวจสอบว่าเป็น Admin หรือเปล่า
MessageBox.Show(“hello Admin”)
Label4.Text = “admin”
Dim hhome As New Home
hhome.Show()
Else
‘ตรวจสอบว่าเป็น User หรือเปล่า
Dim intnumrowuser As Integer
strsqluser = “SELECT COUNT(*) FROM Usertb WHERE [usernameuser]='” & Me.TextBox1.Text & “‘ AND [passworduser] = ‘” & Me.TextBox2.Text & “‘ ”
cmd = New SqlCommand(strsqluser, con)
intnumrowuser = cmd.ExecuteScalar()
If intnumrowuser > 0 Then
Label4.Text = “user”
MessageBox.Show(“คุณคือ USRE”)
Dim hhome As New Home
hhome.Show()
Else
MessageBox.Show(“ไม่มีชื่อคุณในระบบ”)
End If
End If
End Sub
Private Sub TextBox2_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox2.KeyDown
If e.KeyCode = Keys.Enter Then
Button1_Click(sender, e)
End If
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Me.Close()
End Sub
End Class
8. เขียนโค้ดที่ Form Home
Imports System.Data.SqlClient
Imports System.Data
Public Class Home
Dim con As SqlConnection
Dim dbreader As SqlDataReader
Dim results As String
Dim cmd As SqlCommand
Dim strSQL, strsqluser As String
Public Sub Home_Load(sender As Object, e As EventArgs) Handles MyBase.Load
results = “Data Source=HGT02-PC;Initial Catalog=Login;Persist Security Info=True;User ID=sa;Password=123456”
con = New SqlConnection(results)
Try
con.Open()
MsgBox(“Connection Open”)
If Form1.Label4.Text = “admin” Then
Button2.Enabled = False
Label1.Text = “Are you Admin”Else
Label1.Text = “Are you User”
Button1.Enabled = FalseEnd If
Catch ex As Exception
MsgBox(“Can not open connection ! “)
End Try
End Sub
End Class
จบ!!