当我尝试插入我的DataBase时,我得到错误参数'@email'必须定义 我的代码:
MySqlCommand cmd = new MySqlCommand("INSERT INTO users VALUES(@email,@password,@firstName,@LastName,@UserID,@address,@birthday,0)", con);
cmd.Parameters.AddWithValue(@email, email);
cmd.Parameters.AddWithValue(@password, password);
cmd.Parameters.AddWithValue(@firstName, firstName);
cmd.Parameters.AddWithValue(@LastName, LastName);
cmd.Parameters.AddWithValue(@UserID,UserID);
cmd.Parameters.AddWithValue(@address, address);
cmd.Parameters.AddWithValue(@birthday, birthday);
/*THE PROBLEM ISNT IN THE DATABASE,THE FOLOOW INSERT STATMENT WORK!"*/
// MySqlCommand cmd = new MySqlCommand("INSERT INTO users VALUES('evyataresdxez','sdfs','sdfsdf','sdds',33,'sdfsdf','232',0)", con);
con.Open();
cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
我在数据库/代码中没有问题,因为评论中的代码运行良好。感谢
答案 0 :(得分:3)
您需要引用参数名称。现在,您将参数值作为参数名传递:
cmd.Parameters.AddWithValue("email", email);
cmd.Parameters.AddWithValue("password", password);
cmd.Parameters.AddWithValue("firstName", firstName);
cmd.Parameters.AddWithValue("LastName", LastName);
cmd.Parameters.AddWithValue("UserID", UserID);
cmd.Parameters.AddWithValue("address", address);
cmd.Parameters.AddWithValue("birthday", birthday);
这只编译了你的方式,因为变量名前面的@
符号是valid syntax,而你恰好将变量命名为参数名称。