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

标签 c# asp.net query-string

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

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

相关文章:

c# - Request.QueryString ["id"] 和 Request ["id"] 之间的区别

sharepoint - SSRS 在操作链接上插入重复的查询字符串参数

c# - 将 IEnumerable<T> 对象序列化为字符串数组的扩展方法?

c# - 创建 MS Word 文档时如何插入分页符

asp.net - 跨项目分布 ASP.NET 用户控件的技术

c# - 另存为 8 位 PNG

php - Lucene样式查询字符串Elasticsearch PHP

c# - 循环遍历 Razor 中的模型内容

c# - Windows Phone 8 开发和 Web API - 通过表单例份验证进行身份验证?

c# - 从 .EML 文件中检索电子邮件信息