【问题标题】:Return all values from table if query string is empty如果查询字符串为空,则返回表中的所有值
【发布时间】:2010-01-01 11:58:11
【问题描述】:

好的...所以我有一个页面,它根据查询字符串列出表中的产品。

因此,如果我说 foo.aspx?fam=1 将列出家庭 1 的所有产品。

如果查询字符串为空,如何让我的代码列出所有值?

我的 SQL 命令必须有所不同...我真的不知道我该怎么做。

<asp:SqlDataSource ID="ds_produtos" runat="server" 
   ConnectionString="<%$ ConnectionStrings:LocalSqlServer2 %>" 

    SelectCommand="SELECT DISTINCT [IdProduct], [Name], [Description], [IdFamily], [Price],  [Stock] FROM [Product] WHERE ([IdFamily] = @IdFamily )">
    <SelectParameters>
        <asp:QueryStringParameter Name="IdFamily" QueryStringField="fam" Type="Int32" />
    </SelectParameters>
</asp:SqlDataSource>

【问题讨论】:

    标签: c# asp.net query-string


    【解决方案1】:

    为该属性设置一个默认值,以便在您不向页面传递任何值时它具有一个值:

    <asp:QueryStringParameter Name="IdFamily" QueryStringField="fam" Type="Int32" DefaultValue="-1" />
    

    在查询中使用默认值:

    SelectCommand="select IdProduct, [Name], Description, IdFamily, Price, Stock from Product where IdFamily = @IdFamily or @IdFamily = -1"
    

    【讨论】:

      【解决方案2】:

      将您的查询更改为

      SELECT DISTINCT [IdProduct], [Name], [Description], [IdFamily], [Price], [Stock] FROM [Product] WHERE ([IdFamily] = @IdFamily OR @IdFamily Is NULL)

      并在没有为 fam 指定值时传递 null。

      【讨论】:

      • 确保 CancelSelectOnNullParameter 设置为 false。
      猜你喜欢
      • 1970-01-01
      • 2016-10-05
      • 1970-01-01
      • 1970-01-01
      • 2021-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-07
      相关资源
      最近更新 更多