【发布时间】:2013-12-13 14:54:57
【问题描述】:
我正在使用带有 c# 的 SqlDataSource 控件。 ASP.NET 3.5 Visual Studio 2013。
我有两个下拉列表控件,其值用于在 SqlDataSource 查询中使用 ORDER BY 和以下 order by 子句进行排序...
select ... ORDER BY @sortfield @order
@sortfield 是作为过滤器参数的 DropDownList 中的值中的表字段的名称,@order 是从 SqlDataSource 过滤器参数的其他 DropDownList 值中选择的 ASC 或 DESC。
它显然在运行时替换了值,因为返回的错误是:
语法错误:'ASC' 运算符后缺少操作数
否则,如果 FilterParams 字段为空且变量仅放入查询中
@order 运算符后的语法不正确
如果它没有返回错误 - 那么结果集是空的(但是删除 order by 子句并且查询返回一个结果集!)
我尝试添加分号和各种引号和括号,但无济于事。
我也尝试使用设置为 {0} {1} 的 FilterExpression 并使用 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')
按 [@sortfield] [@order] 排序">
这里是使用 {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}">
这个使用上面的 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]">
下拉列表控件:
<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>
【问题讨论】:
-
如果您只显示 SqlDataSource 的代码(而不是对代码进行冗长而复杂的解释),您的问题似乎会简单得多(并且更有可能得到好的答案)。请参阅"on topic" page in the Help Center,上面写着“我们觉得最好的 Stack Overflow 问题中都有一些源代码......”
-
是的。对不起。这是我第一次使用这个论坛。
标签: c# asp.net sql sql-order-by sqldatasource