Login and Register Form (MYSQL)
Posted: Sat Mar 12, 2011 9:42 pm
Free MYSQL Server: http://www.db4free.net
MYSQL Connector download: http://www.mysql.com/downloads/connector/net/
And import the MYSQL Connector.
Go to Project-->Add Reference...---->.NET tab--->Click MySql.Data--->OK
OK to import the MySql.Data.
-Table: user
- Fields: username, password, email
MYSQL Connector download: http://www.mysql.com/downloads/connector/net/
And import the MYSQL Connector.
Go to Project-->Add Reference...---->.NET tab--->Click MySql.Data--->OK
OK to import the MySql.Data.
Code: Select all
Login Form
Imports MySql.Data.MySqlClient
Code: Select all
Register Form
Dim conn As MySqlConnection
'connect to DB
conn = New MySqlConnection()
conn.ConnectionString = "server=82.103.129.94; user id=dbusername; password=dbpassword; database=db"
'see if connection failed.
Try
conn.Open()
Catch myerror As MySqlException
MsgBox("Error connecting to database!")
End Try
'sql query
Dim myAdapter As New MySqlDataAdapter
Dim sqlquery = "SELECT * FROM user WHERE username='" + TextBox1.Text + "' AND password= '" + TextBox2.Text + "'"
Dim myCommand As New MySqlCommand()
myCommand.Connection = conn
myCommand.CommandText = sqlquery
'start query
myAdapter.SelectCommand = myCommand
Dim myData As MySqlDataReader
myData = myCommand.ExecuteReader()
'see if user exists
If myData.HasRows = 0 Then
MsgBox("Invalid username and/or password!")
Code: Select all
MYSQL table and field requirement:Dim conn As MySqlConnection
'connect to DB
conn = New MySqlConnection()
conn.ConnectionString = "server=82.103.129.94; user id=dbusername; password=dbpassword; database=db"
'see if connection failed.
Try
conn.Open()
Catch myerror As MySqlException
MsgBox("Error connecting to database!")
End Try
'sql query
Dim myAdapter As New MySqlDataAdapter
Dim sqlquery = "SELECT * FROM user WHERE username='" + TextBox1.Text + "'"
Dim myCommand As New MySqlCommand()
myCommand.Connection = conn
myCommand.CommandText = sqlquery
'start query
myAdapter.SelectCommand = myCommand
Dim myData As MySqlDataReader
myData = myCommand.ExecuteReader()
'see if user exists
If myData.HasRows = 0 Then
conn.Close()
conn.Open()
Dim registerfinal As New MySqlDataAdapter
sqlquery = "INSERT INTO user (username, password, email) VALUES ('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "')"
myCommand.Connection = conn
myCommand.CommandText = sqlquery
'start query
registerfinal.SelectCommand = myCommand
myData = myCommand.ExecuteReader
MsgBox("Thank you for registering!")
-Table: user
- Fields: username, password, email