我的原始查询使用@WF_ID 作为参数名称,并且可以毫无问题地执行。虽然我很懒,而且 WF_ 输入太多,所以我将参数重命名为 @ID,但现在执行时会抛出 SQLException。确切的消息是“无效的列名'_Page'”
这是查询和相应代码的两个版本。如您所见,唯一改变的是参数名称。
SQL_Connection.Open();
SQL_Command.Connection = SQL_Connection;
SqlParameter param = new SqlParameter("@WF_ID", SqlDbType.Int);
param.Value = WF_ID;
SQL_Command.Parameters.Add(param);
SQL_Command.CommandText = "SELECT COUNT(*) AS MyCount from members WHERE ID = @WF_ID";
int numRows = Convert.ToInt32(SQL_Command.ExecuteScalar().ToString());
SQL_Command.Parameters.Clear();
2.0版破版
SQL_Connection.Open();
SQL_Command.Connection = SQL_Connection;
SqlParameter param = new SqlParameter("@ID", SqlDbType.Int);
param.Value = WF_ID;
SQL_Command.Parameters.Add(param);
SQL_Command.CommandText = "SELECT COUNT(*) AS MyCount from members WHERE ID = @ID";
int numRows = Convert.ToInt32(SQL_Command.ExecuteScalar().ToString());
SQL_Command.Parameters.Clear();
我最初的想法是参数名称不能与列名之一相同......但我找不到任何明确说明的内容。
查看这个 StackOverflow 主题,您似乎无法通过参数传递列名。我可以在 SQL 存储过程中将列名作为输入参数传递吗