简体   繁体   中英

How to get other column value if login successfull in c#

I have one table called Users, which have 4 columns

  • UserId
  • UserName
  • Password
  • Role

If login is successful, I want to know the UserId and Role values ,

for login validate I wrote following function,

 private bool ValidationFunction(string username, string pwd)
    {
        bool boolReturnValue = false;

        string s = "correct connection string";
        SqlConnection con = new SqlConnection(s);
        con.Open();
        string sqlUserName;
        sqlUserName = "SELECT UserName,Password FROM Users WHERE UserName ='" + username + "' AND Password ='" + pwd + "'";
        SqlCommand cmd = new SqlCommand(sqlUserName, con);

        string CurrentName;
        CurrentName = (string)cmd.ExecuteScalar();

        if (CurrentName != null)
        {
            boolReturnValue = true;
        }
        else
        {
            Session["UserName"] = "";
            boolReturnValue = false;
        }
        return boolReturnValue;
    }

ExecuteScalar() function returns only the top record value of the first column . So you need to use ExecuteReader() instead.

Other important thing is you better use a parameterised query to pass those user typed values into the database. You are open for sql injection attacks this way.

Try this:

using (SqlConnection cnn = new SqlConnection("yourConnectionString"))
{
    string sql= "select userId,role from users " +
                "where username=@uName and password=@pWord";

    using (SqlCommand cmd = new SqlCommand(sql,cnn))
    {
         cmd.Parameters.AddWithValue("@uName", username);
         cmd.Parameters.AddWithValue("@pWord", pwd);

         cnn.Open();
         SqlDataReader reader = cmd.ExecuteReader();

         while (reader.Read())
         {
            //get the reader values here.
         }
    }
}

If UserID and Role are in the Users table, you can use the code below. It has the added benefit of protection from SQL injection attacks using parameters.

private class User
{
    public int UserID {get;set;}
    public string Role {get;set;}
    public string UserName {get;set;}

}
private bool ValidationFunction(string username, string pwd, out User)
    {
        bool boolReturnValue = false;

        string s = "correct connection string";
        SqlConnection con = new SqlConnection(s);
        con.Open();
        string sqlUserName;
        sqlUserName = "SELECT UserName,Password,UserID,Role FROM Users WHERE UserName =@usr AND Password=@pwd";
        SqlCommand cmd = new SqlCommand(sqlUserName, con);
        cmd.Parameters.Add(new SqlParameter("usr", username));
        cmd.Parameters.Add(new SqlParameter("pwd", pwd));

        SqlDataReader reader = command.ExecuteReader();

        if (reader.Read())
        {
            boolReturnValue = true;
            User = new User(){UserName = username, UserID=reader.GetInt32(2), Role=reader.GetString(3)};
        }
        else
        {
            Session["UserName"] = "";
            boolReturnValue = false;
        }
        return boolReturnValue;
    }

Use query

SqlDataReaer reader= Select *from Users where password="yourPassword"

and then you can get whatever you want ie reader["userName"] etc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM