简体   繁体   中英

Get Value of specific Column while Login (C# Mysql)

I am trying to make a login at the moment. I watched some videos and found a good way. Every user has an id, Username and Password. I want to get the id of the user who has just been logged in and save it in an Integer. I also tried it with an ExecuteReader but I get an Exception( MySql.Data.MySqlClient.MySqlException ).

My current Code is:

 try
            {
                if (sqlCon.State == ConnectionState.Closed)
                {
                    sqlCon.Open();
                }
                string query = "SELECT COUNT(1) FROM Users_Table WHERE Username=@Username AND Password=@Password;";
                MySqlCommand sqlCmd = new MySqlCommand(query, sqlCon);
                sqlCmd.Parameters.AddWithValue("@Username", maintxtbox1.Text);
                sqlCmd.Parameters.AddWithValue("@Password", Hashed_Password);
                int count = Convert.ToInt32(sqlCmd.ExecuteScalar());
                if (count == 1)
                {
                    //Login correct
                }
                else
                {
                    //Login incorrect
                }
            }
            catch
            {
                //Exception
            }
            finally
            {
                sqlCon.Close();
            }

The try with the ExecuteReader:

try
            {
                if (sqlCon.State == ConnectionState.Closed)
                {
                    sqlCon.Open();
                }
                string query = "SELECT id,Username,Password FROM Users_Table WHERE Username=@Username AND Password=@Password;";
                MySqlCommand sqlCmd = new MySqlCommand(query, sqlCon);
                sqlCmd.Parameters.AddWithValue("@Username", maintxtbox1.Text);
                sqlCmd.Parameters.AddWithValue("@Password", Hashed_Password);

                MySqlDataReader datareader = sqlCmd.ExecuteReader();

                if (datareader.HasRows)
                {
                    MessageBox.Show("Test: " + datareader.GetString("id"));
                }
                else
                {
                    //Login incorrect
                }
            }
            catch
            {
                //Exception
            }
            finally
            {
                sqlCon.Close();
            }

I hope somebody can help me. Thank you in advance.

Try and use the Read method:

if(datareader.Read()){
    MessageBox.Show("Test: "+datareader.GetString(0));
}

EDIT: To make good use and disposal of resources I recommend using the MySqlDataReader inside a using block, ej

using(MySqlDataReader reader = new sqlCmd.ExecuteReader()){
    if(reader.Read()){
        MessageBox.Show("Test: "+reader.GetString(0));
    }
}

I found a solution to my Question by myself. I forgot the while(datareader.Read()) in the ìf(datareader.HasRows) query. Here is my working Code:

try
            {
                if (sqlCon.State == ConnectionState.Closed)
                {
                    sqlCon.Open();
                }
                string query = "SELECT * FROM Users_Table WHERE username=@Username AND password=@Password;";
                MySqlCommand sqlCmd = new MySqlCommand(query, sqlCon);
                sqlCmd.Parameters.AddWithValue("@Username", maintxtbox1.Text);
                sqlCmd.Parameters.AddWithValue("@Password", Hashed_Password);
                MySqlDataReader datareader = sqlCmd.ExecuteReader();
                if (datareader.HasRows)
                {
                    while (datareader.Read())
                    {
                        UserID = datareader.GetInt32("id");
                    }
                }
                else
                {
                    //Incorrect Password
                }

            }
            catch
            {
                //Error
            }
            finally
            {
                sqlCon.Close();
            }

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