You are on page 1of 3

1.

Create database “Example”


2. Create table “tblUsers”
3. In the tblUsers insert the fields id, name,
username,password, role
4. Set id as autoincrement
5. Insert the following data
id Name username password role
1 Manuel Admin 12345 admin
2 Peter Pete 123 secretary
3 Paul Jay Paul 1234 encoder
4 Pauline Pau 4321 encoder

6. Create a log in form

Creating Connection between the Visual studio and the


Database
7. Then click on the project menu.
8. Click Add Reference
9. Then a dialog box will appear
10. Then Click on the Browse Button
11. Then locate MySql.Data.dll
12. After locating, the MySql.Data.dll must be
added on the list, then check it

13. Then on the SOLUTION EXPLORER, add a MODULE, rename it as “connection”


then copy the following code:

Imports MySql.Data.MySqlClient
Module connection
Public cn As New MySqlConnection("Server=localhost; userid=root; password=;
database=YourDatabaseName")
Public cmd As New MySqlCommand
Public da As New MySqlDataAdapter
Public dt As New DataTable
Public dr As MySqlDataReader

Public Sub connect()


If cn.State = ConnectionState.Closed Then
cn.Open()
End If
End Sub

Public Sub disconnect()


If cn.State = ConnectionState.Open Then
cn.Close()
End If
End Sub
End Module

14. Now go back to your log in form design.


15. Then double click the OK button and type the code below, but modify the necessary
modifications needed

Dim i As Integer = 0

cn.Open()
With cmd
.Connection = cn
.CommandText = "SELECT * from tblUsers where username ='" & txtusername.Text & "' and
password='" & txt_password.Text & "'"
End With
dr = cmd.ExecuteReader
dr.Read()
If dr.HasRows Then
Me.Hide()
'MsgBox("Welcome!", MsgBoxStyle.Information, "Successful Login")

nextform.Show()

Else
MsgBox("Incorrect Username and Password", MsgBoxStyle.Critical, "Login")
End If
cn.Close()

You might also like