Iterating through a column in a database

Go To StackoverFlow.com

0

When 'login' button is clicked I would like to iterate through a column in a table and check if a match occurs. How would I go about doing this? I have connected through to a database and I'm reading from database and writing to database fine. I am not sure how I would iterate through a database.

P.S I'm new to both c# and visual studios. I am not having much trouble with C#, since I come over from Java however I'm struggling to get into grips with Visual studios.

2012-04-05 01:45
by user1005253
Write select sql statement - Thit Lwin Oo 2012-04-05 01:48
You mention that you are reading from the database. How exactly are you doing that - Jeremy 2012-04-05 01:49
What tech are you using to access your DB? SqlConnection, linq 2 sql, entity framework ect - Luke McGregor 2012-04-05 01:52
by reading, I only meant displaying. I added sqldatasource and use details/gridview to display the database. On second thought, I suppose this isn't 'reading' from database - user1005253 2012-04-05 01:54
@user1005253 Do you mean you are trying to read all the 'users' from database, and iterate all the records to see if there are a match user? I think it's not a good design. At least it's not suitable for 'login'. You'd better not to read all records. Try to fetch a single user that matches the name and password with a single sql statement - Kirin Yao 2012-04-05 03:15


2

This is simple you'll see.

            SqlConnection myConnection = new SqlConnection(@"Server = (Local); Integrated Security = True;" + "Database = insertDataBaseName"); // Assuming (Local)
            myConnection.Open();
            SqlCommand myCommand = myConnection.CreateCommand();
            myCommand.CommandText = ("SELECT UserName, Password,from Login"); // Where Login is your table . UserName and Password Columns
            SqlDataReader myReader = myCommand.ExecuteReader();
            bool login = false;

            while (myReader.Read())
            {
                if (userNameBox.Text.CompareTo(myReader["UserName"].ToString()) == 0 && passwordBox.Text.CompareTo(myReader["Password"].ToString()) == 0) // A little messy but does the job to compare your infos assuming your using a textbox for username and password
                {
                    login = true;
                }
            }

            if (login)
            {
                      //Your're in.
            }

            else
            {
                MessageBox.Show("Invalid UserName or Password", "Access Denied"); // Error message
            }

                 myReader.Close(); 
                 myConnection.Close(); // Just close everything

Hope this helps. Dont hesitate if you have any question on this code part.

2012-04-05 02:13
by phadaphunk
This is pure reading without generated code. Since you only want to read a SQLDateReader will do the job - phadaphunk 2012-04-05 02:16
the While(Reader.Read()) part is where you actually iterate through your table to check for a match - phadaphunk 2012-04-05 02:18


0

in sql something like this will help

Select top(1) from Users where Id = @Id

or in linq

var user = (from u in users 
            where u.Id == id
            select u).SingleOrDefault();
2012-04-05 01:51
by Luke McGregor


0

If you are chekcing for a username password validation, I think you should not get all user records and loop Iterate thru that. What if you get 100000 user registrations ? You really want to iterate 100000 times ? Really ?

You should probably query for the purticular record you are looking for

Some thing like this

SELECT TOP 1 UserID,FIRSTNAME,LASTNAME,HASHED_PASSWORD,SALT WHERE USERNAME='kristy'

Execute that query againinst your database and see whether you have any records exist, If you have one record present, now you can validate the password with the data you have.

2012-04-05 02:24
by Shyju
Oooh didn't know about this! I guess im the one who learned here. Would have saved me some typing time ; - phadaphunk 2012-04-05 02:32
Ads