search the database?

Lums

Member
Joined
Feb 9, 2010
Messages
22
Programming Experience
Beginner
since u seem to know ur stuff can u help me out with one more thing,

my program has a login form and i have a database with multiple usernames and passwords saved... how would i program it so that it searches through the database for the username and the password and returns whether or not it matches any of those in the database
 
Sure, what I would do is load in all the usernames and passwords, I'd store them in a list of some sort, then when the user types in a username and password, check that list if the username exists and if so, check the password part to see if they match as well. Obviously if either fail then it's not valid.

A Dictionary would be a good object to store the usernames & passwords in, the username would be the key and the password would be the value.
 
It would be much quicker to do a lookup on a Dictionary than querying the database. Which is why I would load the table into memory, unless there's 10,000 records.

If you can't load it into memory like that then use a query similar to this:
VB.NET:
SELECT UserName, Password FROM Users WHERE UserName = ?;
I'm assuming you're using an xsd file for the DB interaction btw. All you would need to do is check if there are any rows returned and if so, check the Password field for a match.
 
VB.NET:
  con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = h:\Docs\College\Computing\CW\Form\VB Forms\VB Forms\Database Design.mdb"
        con.Open()
        sql = "SELECT * FROM [User]"

        da = New OleDb.OleDbDataAdapter(sql, con)
        da.Fill(ds, "Usercont")

        If Usernametxt.Text = ds.Tables("User").Rows(1).Item(3) Then useb = True
        If Passwordtxt.Text = ds.Tables("User").Rows(0).Item(4) Then passb = True


i have been using it like this but it doesnt work
 
VB.NET:
con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = h:\Docs\College\Computing\CW\Form\VB Forms\VB Forms\Database Design.mdb"
Dim ValidLogin As Boolean = False 'Default, keep em out
con.Open()

sql = "SELECT * FROM [User] WHERE UserName = '" & Usernametxt.Text.Trim & "'"

da = New OleDb.OleDbDataAdapter(sql, con)
da.Fill(ds, "Usercont")

con.Close()

With ds.Tables("User")
    If .Rows.Count > 0I Then
        ValidLogin = (Passwordtxt.Text.Trim = Convert.ToString(.Rows(0I).Item(4I)))
    End If
End With

If ValidLogin Then
  'Rest of routine
End If
Though you really should add a DataSet (an xsd file) to your project and use the DataBase through that than doing SQL yourself. You're not even using parametrized queries right now, which is a big no-no in today's world.
 
VB.NET:
con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = h:\Docs\College\Computing\CW\Form\VB Forms\VB Forms\Database Design.mdb"
Dim ValidLogin As Boolean = False 'Default, keep em out
con.Open()

sql = "SELECT * FROM [User] WHERE UserName = '" & Usernametxt.Text.Trim & "'"

da = New OleDb.OleDbDataAdapter(sql, con)
da.Fill(ds, "Usercont")

con.Close()

With ds.Tables("User")
    [B]If .Rows.Count > 0I Then[/B]
        ValidLogin = (Passwordtxt.Text.Trim = [B]Convert.ToString(.Rows(0I).Item(4I)))[/B]    End If
End With

If ValidLogin Then
  'Rest of routine
End If
Though you really should add a DataSet (an xsd file) to your project and use the DataBase through that than doing SQL yourself. You're not even using parametrized queries right now, which is a big no-no in today's world.

i dont get those lines in bold and they arent working either could you annotate for me please
 
glowfoto

Glowfoto :: Photo Sharing
 
Last edited:
Back
Top