【问题标题】:SqlDataSource.FilterParams FilterExpression with ORDER BY CLAUSESqlDataSource.FilterParams FilterExpression 与 ORDER BY CLAUSE
【发布时间】: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


【解决方案1】:

对于初学者,您可能希望用方括号将 @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;
   }

【讨论】:

  • 表名本身是否存储在值中?如果是这样,那么这应该不是问题。基本上,SQLDataSource SelectCommand 中的排序列值似乎没有正确形成,并且没有更大的代码 sn-p 显示完整的 SQLDataSource 和 Dropdown 列表,有很多猜测工作。跨度>
  • 对不起,伙计。先发这个论坛。我已经包含了上面的代码替代方案。它们都不起作用。非常感谢您的回复
  • 你试过代码隐藏吗?我正在编辑我的答案,以举例说明它是如何工作的。
  • 酷 - 我对其进行了更多调整,以便您可以在其中设置一个断点并确认查询语法。
  • 提示。由于某些奇怪的原因,如果您将完整的 table.field 名称作为 DropDownList 控件的值参数放入,则该解决方案不起作用。只有“字段”名称,否则您会收到未知的列错误!修复了它,它运行起来就像一个魅力。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-24
  • 1970-01-01
  • 2013-11-02
  • 1970-01-01
  • 2017-01-11
  • 1970-01-01
相关资源
最近更新 更多