This is the code I am currently running:
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 can log in to my database. The problem is that it only checks login and password. Since both of these work, I don't know how to call another column mgroup_others and check if there is a result of 7 there. I've attached a photo of my database here:
This is the function I rewrote:
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"); } }
P粉4594409912024-01-17 16:57:14
SELECT
The clause should look like this:
SELECT members_pass_hash, mgroup_others
Then, since you are getting multiple values, you should use ExecuteReader()
or use DataAdapter
to Fill()
DataTable
so you can use all values. If you want to be a successful developer, you should write this code yourself so that you understand it, rather than just copying the code we provide you.
P粉5338986942024-01-17 13:58:09
If all you need to know is whether the value in the mgroup_others
column is equal to 7
, you can update the 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 of the other answers) is that you can still use ExecuteScalar since you are only reading a single value.
If you need to retrieve the value of mgroup_others
and inspect it in C# code, you should use the ExecuteReader
or Data Adapter
shown how.