开发者

Parameterized MySql INSERT query not having right parameters value

开发者 https://www.devze.com 2023-02-18 06:42 出处:网络
I\'m trying to do a basic ins开发者_StackOverflow中文版ert 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

I'm trying to do a basic ins开发者_StackOverflow中文版ert 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


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

Try with ? instead of @

0

精彩评论

暂无评论...
验证码 换一张
取 消