2013-11-15 62 views
1

我在C#中使用MySqlParamters時遇到問題。我將在數據庫中創建一個新條目,但MySqlCommand只寫入空值。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 

我能做些什麼?

回答

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" } 
); 
+0

嘿馬克,就是這樣! Thx – Nerospeed