Home >Database >Mysql Tutorial >Why is my C# MySQL Prepared Statement Failing, and How Can I Fix It?
Troubleshooting Prepared Statement in C# with MySQL
You encountered an error while attempting to utilize a prepared statement in your C# program with MySQL. While converting it to a standard statement yielded desired results, you're seeking assistance to identify the issue.
Incorrect Code:
cmd = new MySqlCommand("SELECT * FROM admin WHERE admin_username='@val1' AND admin_password=PASSWORD('@val2')", MySqlConn.conn); cmd.Prepare(); cmd.Parameters.AddWithValue("@val1", tboxUserName.Text); cmd.Parameters.AddWithValue("@val2", tboxPassword.Text);
Corrections:
Corrected Code:
cmd = new MySqlCommand("SELECT * FROM admin WHERE admin_username=@val1 AND admin_password=PASSWORD(@val2)", MySqlConn.conn); cmd.Parameters.AddWithValue("@val1", tboxUserName.Text); cmd.Parameters.AddWithValue("@val2", tboxPassword.Text); cmd.Prepare();
The above is the detailed content of Why is my C# MySQL Prepared Statement Failing, and How Can I Fix It?. For more information, please follow other related articles on the PHP Chinese website!