gpt4 book ai didi

c# - 如果查询字符串为空,则返回表中的所有值

转载 作者:行者123 更新时间:2023-12-03 21:59:15 24 4
gpt4 key购买 nike

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

因此,如果我说 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>

最佳答案

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

<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"

关于c# - 如果查询字符串为空,则返回表中的所有值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1988424/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com