Search code examples
c#mysqlmysql-parameter

Parameterized MySql INSERT query not having right parameters value


I'm trying to do a basic insert statement using a parameterized query. My problem is whatever the syntax I'm using it seems like the query is being done with all parameters set to null instead of their appropriate values (unless I hard-code the value in the command text).

Here is the code :

m_Command.CommandText = "INSERT INTO " + s_TracesTabelName + " (DateTimeTraceCreated,
Span, CenterFrequency, Data, DateTimeTestStarted) VALUES (@traceTime, @Span, 
@CenterFrequency, @Data, @testTime)";


//just trying another syntax
MySqlParameter param = m_Command.CreateParameter();
param.MySqlDbType = MySqlDbType.Datetime;
param.ParameterName = "@traceTime";
param.Value = trace.TimeCreated;
m_Command.Parameters.Add(param);

//m_Command.Parameters.Add("@traceTime",MySqlDbType.Datetime,8,"DateTimeTraceCreated");
//m_Command.Parameters["@traceTime"].Value = trace.TimeCreated;
m_Command.Parameters.Add("@Span", trace.Span);
m_Command.Parameters.Add("@CenterFrequency", trace.CenterFrequency);
m_Command.Parameters.Add("@Data", data);
m_Command.Parameters.Add("@testTime", testStarted);

        try 
        {
            m_Connection.Open();
            m_Command.ExecuteNonQuery();
        }
        catch(Exception e)
        {
            Console.WriteLine("Error Connecting to Database\n");
            //errorlog
        }
        finally
        {
            m_Connection.Close();
        }

The three different syntaxes lead me to null parameters' value.

ps : I've seen many people using command.Parameters.AddWithValue() method, but I seem to just don't have it.

Kind regards, Ben


Solution

  • Could be your version of mysql ADO provider? C# MySqlParameter problem

    Try with ? instead of @