Thursday, June 9, 2011

Visual basic mysql login

Imports MySql.Data.MySqlClient
Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim conn As MySqlConnection
'connect to DB
conn = New MySqlConnection()
conn.ConnectionString = "Server=localhost;User Id=root;Password=;Database=grapescorp"
'see if cennection failed
Try
conn.Open()

Catch myerror As MySqlException
MsgBox("ERROR CONNECTIng to database")

End Try
'
Dim myAdapter As New MySqlDataAdapter

Dim sqlquery = "SELECT * FROM data WHERE id='" + TextBox1.Text + "' AND name='" + 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 exist
If myData.HasRows = 0 Then
MsgBox("Invalid Login")
Else
MsgBox("Login Accepted")
Form2.Show()
Me.Hide()

End If

Me.Close()
End Sub

End Class

No comments:

Post a Comment