MySqlCommand删除了所有参数值

时间:2013-11-15 11:03:50

标签: c# mysql windows

我在C#中遇到MySqlParamters的问题。我将在数据库中创建一个新条目,但MySqlCommand只写入空值。

    MySqlCommand see = new MySqlCommand("REPLACE INTO users (login, email) VALUES (@login,@email)",this.dbcontext);

            MySqlParameter p1 = new MySqlParameter("@login",MySqlDbType.VarChar,20);
            p1.Value = "Hello";

MySqlParameter p2 = new MySqlParameter("@email", "Dude");

            see.Parameters.Add(p1);
            see.Parameters.Add(p2);

            Console.WriteLine(p2.Value);

            see.ExecuteNonQuery();

我的数据库中的条目是

login   email
------------------
null    null

我该怎么办?

2 个答案:

答案 0 :(得分:0)

MySql连接器是否使用?作为参数? (@是用户变量) - 所以:

using(var see = new MySqlCommand(
    "REPLACE INTO users (login, email) VALUES (?login,?email)",this.dbcontext))
{
    MySqlParameter p1 = new MySqlParameter("login",MySqlDbType.VarChar,20);
    p1.Value = "Hello";
    MySqlParameter p2 = new MySqlParameter("email", "Dude");
    see.Parameters.Add(p1);
    see.Parameters.Add(p2);
    see.ExecuteNonQuery();
}

或者如果你喜欢“dapper”:

this.dbcontext.Execute(
    "REPLACE INTO users (login, email) VALUES (?login,?email)",
    new { login = "Hello", email = "Dude" }
);

答案 1 :(得分:-1)