重组 SQL 查询但不显示元素

Restructured SQL Query but it does not display elements

我有一个下拉列表 ddlProjecsFilter,其中填充了我数据库中所有项目的名称。

在页面加载时,我会显示此下​​拉列表,当有人选择一个名称时,他们会获得有关该项目名称的所有信息。但是,我有两个基于字符输入的额外过滤器(Funder、Type)。

像这样编写查询时,下拉列表中没有任何元素,但其他两个过滤器有效。

  protected void nameFilter()
    {
        string queryDefault = "SELECT NULL AS [Value], 'All' AS [Text], '0' AS [OrderNo] UNION ALL ";
        string queryBase = "SELECT [NameId] AS [Value], [Type] AS [Text], '1' AS [OrderNo] FROM [tblProjects] ";
        string queryWhere = " WHERE NameId IS NOT NULL ";
        string queryOrder = " ORDER BY [OrderNo], [Text]";


        if (!string.IsNullOrEmpty(txtTypeFilter.Text))
        {
            queryWhere +=" AND [Funder] LIKE @Funder";
            queryWhere += " AND [Type] LIKE @Type";
        }

        sdsProjects.SelectCommand = queryDefault + queryBase + queryWhere + queryOrder;
        sdsProjects.SelectParameters.Add("@Funder", txtFunderFilter.Text);
        sdsProjects.SelectParameters.Add("@Type", txtTypeFilter.Text);

        ddlProjectsFilter.DataBind();
    }

如果我这样做,一切都会按预期进行。但是,这不是最佳做法。

   if (!string.IsNullOrEmpty(txtTypeFilter.Text))
        {
            queryWhere += string.Format(" AND [Funder] LIKE'%{0}%'", txtFunderFilter.Text);
            queryWhere += string.Format(" AND [Type] LIKE'%{0}%'", txtTypeFilter.Text);
        }

谁能指出第一个片段的问题?

定义:

 <td>
                                        <asp:DropDownList runat="server" DataSourceID="sdsProjects" ID="ddlProjectsFilter"
                                            AutoPostBack="true" DataTextField="Text" DataValueField="Value"
                                            CssClass="form-control local-control" OnSelectedIndexChanged="ddlProjectsFilter_SelectedIndexChanged" AppendDataBoundItems="true">
                                        </asp:DropDownList><asp:SqlDataSource ID="sdsProjectsFilter" runat="server" ConnectionString="<%$ ConnectionStrings:DatabaseLive %>"></asp:SqlDataSource>
                                    </td>
                                    <td></td>
                                    <td>
                                        <asp:TextBox runat="server" ID="txtTypeFilter" AutoPostBack="true" OnTextChanged="txtTypeFilter_TextChanged"
                                            CssClass="form-control local-control TypeFilter"></asp:TextBox></td>
                                    <td></td>
                                    <td>
                                        <asp:TextBox runat="server" ID="txtFunderFilter" AutoPostBack="true" OnTextChanged="txtFunderFilter_TextChanged"
                                            CssClass="form-control local-control FunderFilter"></asp:TextBox></td>...

你的设置很好。但是,您应该在每次希望添加可选参数时“配对”文本框测试。

因此,通过此设置,您可以有 1 或 5 个可选过滤器。

因为你想要一个赞,请看下面的代码:

所以,我建议:

        SqlDataSource sdsProjects = new SqlDataSource();

        string queryDefault = "SELECT NULL AS [Value], 'All' AS [Text], '0' AS [OrderNo] UNION ALL ";
        string queryBase = "SELECT [NameId] AS [Value], [Type] AS [Text], '1' AS [OrderNo] FROM [tblProjects] ";
        string queryWhere = " WHERE NameId IS NOT NULL ";
        string queryOrder = " ORDER BY [OrderNo], [Text]";


        if (!string.IsNullOrEmpty(txtTypeFilter.Text))
        {
            queryWhere += " AND ([Type] LIKE '%' + @Type + '%') ";
            sdsProjects.SelectParameters.Add("@Type",DbType.String, txtTypeFilter.Text);
        }

        if (!string.IsNullOrEmpty(txtFunderFilter.Text))
        {
            queryWhere += " AND ([Funder] LIKE '%' + @Funder + '%') ";
            sdsProjects.SelectParameters.Add("@Funder",DbType.String, txtFunderFilter.Text);
        }

        sdsProjects.SelectCommand = queryDefault + queryBase + queryWhere + queryOrder;