by Naveen (1 Submission)
Category: Databases/Data Access/DAO/ADO
Compatability: VB.NET
Difficulty: Unknown Difficulty
Originally Published: Mon 14th March 2011
Date Added: Mon 8th February 2021
Rating: (1 Votes)
Checks a database and logins
'The database used is C:\db\logindb.mdb .The database contains "userlogin" as the table which contains username AND password as the two fields..
'Change the SQL query to your needs, i got the code from http://www.w3schools.com/sql/default.asp it has all the code we can use :)
'I am using MS Visual Basic 2010 Express
Imports System.Data.OleDb
Public Class Form1
'con is the connection made to the database
Dim con As OleDbConnection = New OleDbConnection("Provider=Microsoft.jet.oledb.4.0;data source=c:\db\logindb.mdb") 'change the source to where your database is situated...
Dim cmd As OleDbCommand
Dim myDA As OleDbDataAdapter
Dim myDataSet As DataSet
Dim query As String
Dim flag As Boolean
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
query = "Select * FROM userlogin WHERE username = '" & TextBox1.Text & "' AND password = '" & TextBox2.Text & "';" 'Change the sql query for different names of tables or the fields..
cmd = New OleDbCommand(query)
cmd.Connection = con
If con.State = ConnectionState.Closed Then con.Open()
Dim dr As OleDbDataReader = cmd.ExecuteReader
While dr.Read()
If dr.HasRows() = True Then
flag = True
MsgBox("Login Successful")
Form2.Show()
Me.Hide()
End If
End While
If flag <> True Then ' Couldn't work a way to show login failed in the While loop itself. Try it if you can...
MsgBox("Login Failed")
End If
End Sub
'I hope this will encourage you to make more tutorials, and let this be the next so you could continue your database topic (stretch)...
'Sorry if my english is bad.. I am an Indian. [email protected] youtube: o0ae0o skype: o0ae0o
End Class