1

MySqlParamters我在 C# 中有一个问题。我将在数据库中创建一个新条目,但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

我能做些什么?

4

2 回答 2

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();
}

或者如果你喜欢一些“精致”:

this.dbcontext.Execute(
    "REPLACE INTO users (login, email) VALUES (?login,?email)",
    new { login = "Hello", email = "Dude" }
);
于 2013-11-15T11:20:32.133 回答