Click here to Skip to main content
15,920,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a graphical interface it has UserID and password texts.I want to store userId 's and passwords on sql server then control it on program.Which way i use ? I am new at SQL so dont know much more.Is there any sql query to control it with using database ?
Posted

There is a tip here that tells you the basics of password storage: Password Storage: How to do it.[^] - it doesn't tell you how to actually store it in a database, but that is the easy bit!

Read from DB:
using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand com = new SqlCommand("SELECT password FROM loginData WHERE userID=@ID", con))
        {
        com.Paramaters.AddWithValue("@ID", userID);
        using (SqlDataReader reader = com.ExecuteReader())
            {
            if (reader.Read())
                {
                byte[] password = (byte[]) reader["password"];
                return password;
                }
            }
        }
    }
Write To DB:
using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand com = new SqlCommand("INSERT INTO loginData (userID, password) VALUES (@ID, @PW)", con))
        {
        com.Parameters.AddWithValue("@ID", userID);
        com.Parameters.AddWithValue("@PW", password);
        com.ExecuteNonQuery();
        }
    }
 
Share this answer
 
Comments
omyildirim 21-Jul-11 3:32am    
Thanx it is so clean
Hi,

Make a table in SQL server with UserID and password field on it,Since you are new at SQL so you dont have to need to encrypt or decrypt passwords

at Login read the userid and password like

Select * from table where UserID='"+uid+"' and password='"+pwd+"'";
 
Share this answer
 
Comments
omyildirim 21-Jul-11 3:28am    
Thanks for suggesting.Because of i am new at sql dont know which way i follow.So can you suggest some tricks to me ?
omyildirim 21-Jul-11 3:31am    
Also how can i understand if that query returns true or false ?
Syed Salman Raza Zaidi 21-Jul-11 3:58am    
using (SqlDataReader reader = com.ExecuteReader())
{
if (reader.Read())
{
byte[] password = (byte[]) reader["password"];
Response.Redirect("Home.aspx");
}
else

Response.Write("Invalid username or password")
}
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900