MySqlCommand.Command.Parameters.Add Obsolete Warning: Transition to AddWithValue
Question:
While using MySqlCommand to insert data into a MySQL database, you receive a warning indicating that "MySqlCommand.Command.Parameters.Add is obsolete" and the parameter values are not being inserted correctly. How do you resolve this issue and ensure SQL injection safety?
Answer:
To address the warning and improve SQL injection prevention, you should transition from using "Add" to "AddWithValue" for adding parameters to your MySqlCommand.
Modified Code:
... command.Parameters.AddWithValue("@mcUserName", mcUserNameNew); command.Parameters.AddWithValue("@mcUserPass", mcUserPassNew); command.Parameters.AddWithValue("@twUserName", twUserNameNew); command.Parameters.AddWithValue("@twUserPass", twUserPassNew); ...
Additional Considerations:
string SQL = "INSERT INTO `twMCUserDB` (`mc_userName`, `mc_userPass`, `tw_userName`, `tw_userPass`) VALUES (@mcUserName, @mcUserPass, @twUserName, @twUserPass)";
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3