string selectedAreas = getSelectedAreas(areaCounts);
SqlConnection cn = new SqlConnection(connectionstring);
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "select top 1 [x1] " +
"from sometable " +
"where sometable.coll = @selectedAreas" +
"order by NEWID() ";
cmd.CommandType = System.Data.CommandType.Text;
cmd.Connection = cn;
cmd.Parameters.AddWithValue("@selectedAreas", selectedAreas);
我在这里做错了什么?
我明白了
必须为@selectedAreas 声明标量变量。
@selectedAreas
可能会变成这样:
" 'nyc' or sometable.coll = 'la' or sometable.coll = 'miami' "
编辑:
正如下面的评论指出的那样,我添加了空格。并删除了参数,如下所示:
cmd.CommandText = "select top 1 [x1] " +
"from sometable " +
"where sometable.coll = " + selectedAreas
" order by NEWID() ";
不知道它有多正确,但它现在有效......