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:
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
The
SELECT
clause should look like this:Then, since you are getting more than one value, you should use either
ExecuteReader()
or using aDataAdapter
toFill()
aDataTable
, 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.It appears the mentioned SQL query is wrong, so change your SELECT query as follows:
Refer to this doc to understand how to write proper MySql queries.
If all you need to know is if the value in the
mgroup_others
column is equal to7
, you can update your SQL as follows: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 useExecuteReader
or aDataAdapter
.