skip to Main Content

Here is my current running code:

 private void button1_Click(object sender, EventArgs e)
    {
        string server = "207.244.70.217";
        string database = "nextgene_NGH";
        string uid = "EXAMPLEUID";
        string password = "EXAMPLEPASSWORD";
        string ssl = "None";

        string connectionString = $"SERVER={server};DATABASE={database};UID={uid};PASSWORD={password};SSL Mode={ssl};";    
        using var connection = new MySqlConnection(connectionString);
        connection.Open();
        using var command = new MySqlCommand("SELECT members_pass_hash FROM core_members && WHERE name = @name;", connection);
        command.Parameters.AddWithValue("@name", maskedTextBox1.Text);
        var hashedPassword = (string) command.ExecuteScalar();
        if (!(hashedPassword is null && !BCrypt.Net.BCrypt.Verify(maskedTextBox2.Text, hashedPassword)))
        {
            
            MessageBox.Show("Login Successful");
        }
    }

The above code works to login my database. The problem is it only checks to see the login name and the password. As these two work, i can’t figure out how to call another column mgroup_others and check to see if there is a result of 7 there or not. I have attached a photo of my database here:

mgroup_others

Here is my rewritten function:

   private void button1_Click(object sender, EventArgs e)
    {
        string server = "207.244.70.217";
        string database = "nextgene_NGH";
        string uid = "EXAMPLEUID";
        string password = "EXAMPLEPASSWORD";
        string ssl = "None";
        string vipcheck = "7";

        string connectionString = $"SERVER={server};DATABASE={database};UID={uid};PASSWORD={password};SSL Mode={ssl};";    
        using var connection = new MySqlConnection(connectionString);
        connection.Open();
        using var command = new MySqlCommand("SELECT members_pass_hash AND mgroup_others FROM core_members && WHERE name = @name;", connection);
        command.Parameters.AddWithValue("@name", maskedTextBox1.Text);
        var hashedPassword = (string) command.ExecuteScalar();
        if (!(hashedPassword is null && !BCrypt.Net.BCrypt.Verify(maskedTextBox2.Text && vipcheck != mgroup_others, hashedPassword)))
        {
            
            MessageBox.Show("Login Successful");
        }
    }

3

Answers


  1. The SELECT clause should look like this:

    SELECT members_pass_hash, mgroup_others
    

    Then, since you are getting more than one value, you should use either ExecuteReader() or using a DataAdapter to Fill() a DataTable, so you can use all the value. If you want to have success as a developer, this is code you should write yourself, so you understand it, rather than just copying what we give you.

    Login or Signup to reply.
  2. It appears the mentioned SQL query is wrong, so change your SELECT query as follows:

    SELECT members_pass_hash, mgroup_others FROM core_members WHERE name = @name;
    

    Refer to this doc to understand how to write proper MySql queries.

    Login or Signup to reply.
  3. If all you need to know is if the value in the mgroup_others column is equal to 7, you can update your SQL as follows:

    using var command = new MySqlCommand("""
        SELECT members_pass_hash
        FROM core_members
        WHERE name = @name AND mgroup_others = 7;
        """, connection);
    command.Parameters.AddWithValue("@name", maskedTextBox1.Text);
    var hashedPassword = (string) command.ExecuteScalar();
    

    The advantage of this approach (compared to some other answers) is that you can still use ExecuteScalar because you’re only reading a single value.

    If you need to retrieve the value of mgroup_others and examine it in your C# code, you should use one of the other answers that shows how to use ExecuteReader or a DataAdapter.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search