我一直在尝试使用以下参数作为sql查询传递变量值:
SELECT TOP 5 *
FROM Events
WHERE EID NOT IN (SELECT TOP @limit EID FROM Events ORDER BY EID)
用于在参数@limit
的值指定的前n行之后查找记录。
E.g。如果我指定值10,则查询返回第6-10行。
现在我正在做的是
string str = "SELECT TOP 5 * FROM Events WHERE EID NOT IN (SELECT TOP @limit EID FROM Events ORDER BY EID)"
SqlCommand cmd=new SqlCommand(str,con); // con is the connection string
cmd.Parameters.AddWithValue("limit", value);
SqlDataAdapter sda= new SqlDataAdapter(cmd);
DataSet ds=new DataSet();
sda.Fill(ds);
return ds;
然后将数据集ds
绑定到Datalist
。
目前引发错误
“@limit附近的语法不正确”
代码有什么问题??
答案 0 :(得分:6)
使用参数提取TOP n
时,必须将参数括在括号中:
string str= "SELECT TOP 5 * FROM Events WHERE EID NOT IN (SELECT TOP (@limit) EID FROM Events ORDER BY EID)"