I keep getting this error: MySql.Data.MySqlClient.MySqlException: 'You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''Feature: SecurityTesting @mytag Scenario: SQL Injection Given I visit ' at line 1'
The parameter Gherkin is the one causing the query to fail. I have tried both ? and @ as parameter prefixes and nothing changes.
Here's my code:
string CommandText = " INSERT INTO Feature(`Path`, Gherkin, RepoID, `Name`, Updated) VALUES (?Path, ?Gherkin , ?RepoID, ?Name, ?Updated) ON DUPLICATE KEY UPDATE Gherkin = VALUES(?Gherkin); ";
using (MySqlConnection connection = new MySqlConnection())
{
connection.ConnectionString = ConfigurationManager.ConnectionStrings["TAF_DB"].ConnectionString;
using (MySqlCommand command = new MySqlCommand())
{
var gherkinParam = new MySqlParameter("Gherkin", test.Gherkin);
//var gherkinParam = new MySqlParameter("Gherkin", MySqlDbType.VarChar);
var pathParam = new MySqlParameter("Path", MySqlDbType.VarChar);
var RepoIDParam = new MySqlParameter("RepoID", MySqlDbType.Int64);
var nameParam = new MySqlParameter("Name", MySqlDbType.VarChar);
var updatedParam = new MySqlParameter("Updated", MySqlDbType.VarChar);
gherkinParam.Value = test.Gherkin;
command.Parameters.Add(gherkinParam);
pathParam.Value = test.Path;
command.Parameters.Add(pathParam);
RepoIDParam.Value = test.RepoID;
command.Parameters.Add(RepoIDParam);
nameParam.Value = test.Name;
command.Parameters.Add(nameParam);
updatedParam.Value = test.Updated;
command.Parameters.Add(updatedParam);
command.Connection = connection;
command.CommandType = CommandType.Text;
command.CommandText = CommandText;
connection.Open();
command.ExecuteNonQuery();
connection.Close();
}
}
command.CommandType = CommandType.Text;is not necessary, it's the default.connection.Close();also not necessary,usingwill do that. You can use construcotr parameters to shorten tousing (var connection = new MySqlConnection(ConfigurationManager.ConnectionStrings["TAF_DB"].ConnectionString)) using (var command = new MySqlCommand(CommandText, connection)) {...you can also shorten the parameters tocommand.Parameters.Add(new MySqlParameter("@Gherkin", MySqlDbType.VarChar).Value = test.Gherkin;etc. You can use a multi-line string for the command-text with@"