我正在使用带有c#的SqlDataSource控件。 ASP.NET 3.5 Visual Studio 2013。
我有两个下拉列表控件,其中包含使用ORDER BY进行排序的值,以及SqlDataSource查询中的以下order by子句...
select ... ORDER BY @sortfield @order
@sortfield是DropDownList中作为Filterparameter的值的表字段的名称,而@order是从SqlDataSource FilterParameter的其他DropDownList值中选择的ASC或DESC。
它明确地在运行时替换值,因为返回的错误是:
语法错误:'ASC'运算符后缺少操作数
或者,如果FilterParams字段留空并且变量只放在查询中
如果它没有返回错误 - 那么结果集为空(但是删除order by子句并且查询返回结果集!)
我尝试添加分号和各种引号和括号,但无济于事。
我还尝试将FilterExpression设置为{0} {1}并使用ORDER BY结束查询 - 但这更糟糕。
为什么语法错误?使用带有ORDER BY子句的FilterParams是否有问题?
这是SqlDataSource代码。注意我这里没有使用过滤器表达式,但这似乎更糟糕了:
<asp:SqlDataSource ID="sqlds_copy_1" runat="server" ConnectionString="<%$ ConnectionStrings:abConnectionString %>"
OnInit="sqlds_copy_1_Init" OnPreRender="sqlds_copy_1_PreRender"
SelectCommand="SELECT ab_genericentry.title, ab_genericentry.heading, ab_genericentry.body, ab_genericentry.time, ab_genericentry.date, ab_genericentry.header, ab_genericentry.synopsis, ab_genericentry.abstract, ab_genericentry.footer, ab_genericentry.introduction, ab_genericentry.conclusion, ab_genericentry.headline, ab_genericentry.release_date, ab_copyinstances.control_name FROM ab_pages INNER JOIN ab_sites ON ab_pages.site_id = ab_sites.id INNER JOIN ab_copyinstances ON ab_pages.id = ab_copyinstances.page_id AND ab_sites.id = ab_copyinstances.site_id INNER JOIN ab_genericentry ON ab_copyinstances.id = ab_genericentry.copy_instance_id WHERE (ab_sites.site_name LIKE 'philaxiom') AND (ab_copyinstances.control_name LIKE 'dl_copy_1') AND (ab_pages.page_url LIKE '/pa_home.aspx')
ORDER BY [@sortfield] [@order]“&gt;
这里使用的是使用{0} {1}的过滤器表达式,但这会在第二个参数上使用: 语法错误:'ASC'运算符后缺少操作数。
所以FilterParams值会进入,但解析器不喜欢表达式的结尾。我不知道为什么。
我再次使用下拉列表中的值字段,但SqlDataSource FilterParameters属性中的向导似乎只允许使用值 - 而不是文本:
<asp:SqlDataSource ID="sqlds_copy_1" runat="server" ConnectionString="<%$ ConnectionStrings:abConnectionString %>"
OnInit="sqlds_copy_1_Init" OnPreRender="sqlds_copy_1_PreRender"
SelectCommand="SELECT ab_genericentry.title, ab_genericentry.heading, ab_genericentry.body, ab_genericentry.time, ab_genericentry.date, ab_genericentry.header, ab_genericentry.synopsis, ab_genericentry.abstract, ab_genericentry.footer, ab_genericentry.introduction, ab_genericentry.conclusion, ab_genericentry.headline, ab_genericentry.release_date, ab_copyinstances.control_name FROM ab_pages INNER JOIN ab_sites ON ab_pages.site_id = ab_sites.id INNER JOIN ab_copyinstances ON ab_pages.id = ab_copyinstances.page_id AND ab_sites.id = ab_copyinstances.site_id INNER JOIN ab_genericentry ON ab_copyinstances.id = ab_genericentry.copy_instance_id WHERE (ab_sites.site_name LIKE 'philaxiom') AND (ab_copyinstances.control_name LIKE 'dl_copy_1') AND (ab_pages.page_url LIKE '/pa_home.aspx')
ORDER BY“FilterExpression =”{0} {1}“&gt;
这个使用上面的FilterParams属性,但使用[@sortfield] [@order]。 IOt给出了语法错误:'[@order]'运算符后缺少操作数:
<asp:SqlDataSource ID="sqlds_copy_1" runat="server" ConnectionString="<%$ ConnectionStrings:abConnectionString %>"
OnInit="sqlds_copy_1_Init" OnPreRender="sqlds_copy_1_PreRender"
SelectCommand="SELECT ab_genericentry.title, ab_genericentry.heading, ab_genericentry.body, ab_genericentry.time, ab_genericentry.date, ab_genericentry.header, ab_genericentry.synopsis, ab_genericentry.abstract, ab_genericentry.footer, ab_genericentry.introduction, ab_genericentry.conclusion, ab_genericentry.headline, ab_genericentry.release_date, ab_copyinstances.control_name FROM ab_pages INNER JOIN ab_sites ON ab_pages.site_id = ab_sites.id INNER JOIN ab_copyinstances ON ab_pages.id = ab_copyinstances.page_id AND ab_sites.id = ab_copyinstances.site_id INNER JOIN ab_genericentry ON ab_copyinstances.id = ab_genericentry.copy_instance_id WHERE (ab_sites.site_name LIKE 'philaxiom') AND (ab_copyinstances.control_name LIKE 'dl_copy_1') AND (ab_pages.page_url LIKE '/pa_home.aspx')
ORDER BY“FilterExpression =”[@ sortfield] [@order]“&gt;
DROP DOWN LISTS CONTROLS:
<asp:DropDownList ID="ddl_SortField" runat="server" AppendDataBoundItems="True">
<asp:ListItem Value="ab_genericentry.title" Selected="True">Entry Name</asp:ListItem>
<asp:ListItem Value="ab_genericentry.date">Entry Date</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="ddl_Order" runat="server">
<asp:ListItem Value="DESC">↓</asp:ListItem>
<asp:ListItem Value="ASC" Selected="True">↑</asp:ListItem>
</asp:DropDownList>
答案 0 :(得分:0)
对于初学者,您可能希望用方括号括起@sortfield,以消除列名中有空格导致语法错误(Missing operand after 'Operator Name' operator)的可能性。
另外,您是使用下拉框中的正确值设置参数(即文本而不是值)?不幸的是,如果没有能够看到更多的代码,很难给出更直接的答案。
代码隐藏示例:
1)将更改事件添加到排序下拉列表
asp:DropDownList ID =“ddl_SortField”runat =“server”AppendDataBoundItems =“True”OnSelectedIndexChanged =“ddl_SortField_SelectedIndexChanged”
2)代码隐藏
protected void ddl_SortField_SelectedIndexChanged(object sender, EventArgs e) { var sqlCommand = string.Format("SELECT ab_genericentry.title, ab_genericentry.heading, ab_genericentry.body, " + "ab_genericentry.time, ab_genericentry.date, ab_genericentry.header, ab_genericentry.synopsis, " + "ab_genericentry.abstract, ab_genericentry.footer, ab_genericentry.introduction, ab_genericentry.conclusion, " + "ab_genericentry.headline, ab_genericentry.release_date, ab_copyinstances.control_name " + "FROM ab_pages " + "INNER JOIN ab_sites ON ab_pages.site_id = ab_sites.id " + "INNER JOIN ab_copyinstances ON ab_pages.id = ab_copyinstances.page_id AND ab_sites.id = ab_copyinstances.site_id " + "INNER JOIN ab_genericentry ON ab_copyinstances.id = ab_genericentry.copy_instance_id " + "WHERE (ab_sites.site_name LIKE 'philaxiom') AND (ab_copyinstances.control_name LIKE 'dl_copy_1') " + "AND (ab_pages.page_url LIKE '/pa_home.aspx')" + "ORDER BY [{0}] {1}", ddl_SortField.SelectedValue.ToString(), ddl_Order.SelectedValue.ToString()); sqlds_copy_1.SelectCommand = sqlCommand; }