[英]Invalid column name SQL
I have wasted hours trying to find what's incorrect in my code. 我浪费了很多时间试图找出我的代码中不正确的地方。 Before adding a new column in the table, it was ok but after that, whenever I submit my form, it gives me an error
在表中添加新列之前,可以,但是此后,每当我提交表单时,都会给我一个错误
The column name is not valid.
列名无效。 [ Node name (if any) = ,Column name = userID ]
[节点名称(如果有)=,列名称= userID]
Here is my code: 这是我的代码:
con.Open();
string query = "INSERT INTO PlayerTable
(username ,password, picture, scoreL1, scoreL2, scoreL3, userID)
VALUES
('" + @userName + "','" + @password + "',@picture," + @score1
+ "," + @score2 + "," + @score3 + "," + @num + ")";
cmd.Parameters.AddWithValue("@picture", a);
cmd.Parameters.AddWithValue("@username", userName);
cmd.Parameters.AddWithValue("@password", password);
cmd.Parameters.AddWithValue("@scoreL1", score1);
cmd.Parameters.AddWithValue("@scoreL2", score2);
cmd.Parameters.AddWithValue("@scoreL3", score3);
cmd.Parameters.AddWithValue("@userID", num);
cmd = new SqlCeCommand(query, con);
cmd.ExecuteNonQuery();
Shouldn't it be: 不应该是:
string query =
"INSERT INTO PlayerTable
(username, password, picture, scoreL1, scoreL2, scoreL3, userID)
VALUES(@userName, @password, @picture, @scoreL1, @scoreL2, @scoreL3, @userID)";
That is not @score1
but @scoreL1
and same for the others. 那不是
@score1
而是@scoreL1
,其他的都一样。
Edit 编辑
When you instantiate a new SqlCeCommand
: 实例化新的
SqlCeCommand
:
cmd = new SqlCeCommand(query, con);
you essentially erase the paramaters you set earlier. 实际上,您将删除先前设置的参数。
Move the instantiation above the parameter assignments: 将实例移到参数分配上方:
cmd = new SqlCeCommand(query, con);
cmd.Parameters.AddWithValue("@picture", a);
cmd.Parameters.AddWithValue("@username", userName);
cmd.Parameters.AddWithValue("@password", password);
cmd.Parameters.AddWithValue("@scoreL1", score1);
cmd.Parameters.AddWithValue("@scoreL2", score2);
cmd.Parameters.AddWithValue("@scoreL3", score3);
cmd.Parameters.AddWithValue("@userID", num);
cmd.ExecuteNonQuery();
You should not be surrounding your query parameters with quotes: 您不应在查询参数中用引号引起来:
string query = "INSERT INTO PlayerTable
(username ,password, picture, scoreL1, scoreL2, scoreL3, userID)
VALUES(@userName, @password, @picture, @scoreL1, @scoreL2, @scoreL3, @userID )";
Additionally, you need to make sure your Parameter Names match the @ values. 此外,您需要确保您的参数名称与@值匹配。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.