基于多个下拉选择的sql语句

时间:2015-11-24 14:35:55

标签: c# sql asp.net

我需要创建一个可以根据所选的选项进行调整的SQL语句。我有2个下拉列表从数据库中填充。

这是我的ASP

<td class="theSearchTableData">
                    <asp:UpdatePanel runat="server" ID="up_team" UpdateMode="Conditional">
                        <ContentTemplate>
                            <asp:DropDownList runat="server" ID="ddl_team"></asp:DropDownList><%--AutoPostBack="true" OnSelectedIndexChanged="ddl_team_SelectedIndexChanged"--%>
                        </ContentTemplate>
                    </asp:UpdatePanel>

                </td>
                <td class="theSearchTableData">
                    <asp:DropDownList runat="server" ID="ddl_owner"></asp:DropDownList>
                </td>
                  <td class="theSearchTableData">
                    <asp:DropDownList runat="server" ID="ddl_actionowner" ></asp:DropDownList>
                </td>

c#c​​ode

string Owner_ActionOwner_Combo = "";
string ownerClause = "";
string ActionownerClause = "";

    if (ddl_owner.SelectedIndex > 0)
    {
        ownerClause = " log_number in (SELECT DISTINCT log_number FROM Log_Data WHERE log_owner = @logOwner) AND ";
    }

    if (ddl_actionowner.SelectedIndex > 0)
    {
        ActionownerClause = " log_number in (SELECT DISTINCT log_number FROM Log_Data WHERE log_owner <> @actionOwner and current_action_owner = @actionOwner  ) AND ";
    }

    else if (ddl_actionowner.SelectedIndex >0 & ddl_owner.SelectedIndex > 0 )
    {
        Owner_ActionOwner_Combo = "log_number in (SELECT DISTINCT log_number FROM Log_Data WHERE current_action_owner = @actionOwner and log_owner =@logOwner) AND";
    }
string completeWhereClause = ActionownerClause + Owner_ActionOwner_Combo + ownerClause;

'@'值已经参数化。

log_owner通常是action_owner,但是有一段时间log_owner与action_owner不同。这就是为什么我需要调整搜索标准。 如果用户想知道他们由日志所有者搜索的日志所有者,并且与操作所有者相同。问题是当他们两者都搜索时。

我已将其输入我的数据库并获取正确的数字。对于log_owner,有32个,如果只有action_owner有3个,那么组合应该有24个。

感谢您的时间

1 个答案:

答案 0 :(得分:1)

你的if语句结构的方式转换为这个..

if (ddl_owner.SelectedIndex > 0)
{   Hit if ddl_owner.SelectedIndex > 0       }
if (ddl_actionowner.SelectedIndex > 0)
{   Hit if ddl_actionowner.SelectedIndex > 0       }
else if (ddl_actionowner.SelectedIndex > 0 & ddl_owner.SelectedIndex > 0)
{   Never hit        }

你可能应该将你的组合移动到顶部,如果是另外两个则使用else

string Owner_ActionOwner_Combo = "";
string ownerClause = "";
string ActionownerClause = "";

if (ddl_actionowner.SelectedIndex > 0 && ddl_owner.SelectedIndex > 0)
{
    Owner_ActionOwner_Combo = "log_number in (SELECT DISTINCT log_number FROM Log_Data WHERE current_action_owner = @actionOwner and log_owner =@logOwner) AND";
}
else if (ddl_owner.SelectedIndex > 0)
{
    ownerClause = " log_number in (SELECT DISTINCT log_number FROM Log_Data WHERE log_owner = @logOwner) AND ";
}
else if (ddl_actionowner.SelectedIndex > 0)
{
    ActionownerClause = " log_number in (SELECT DISTINCT log_number FROM Log_Data WHERE current_action_owner = @actionOwner  ) AND ";
}
string completeWhereClause = ActionownerClause + Owner_ActionOwner_Combo + ownerClause;