多个可选的WHERE参数

时间:2014-06-30 15:28:40

标签: c# sql sql-server

我想为一组SQL查询创建一个过滤器,以便用户可以最多三个值过滤SQL SELECT。 用户界面有三个文本框,每个文本框将绑定到SQL表中的列名。用户可以通过这些文本框提供一个,两个或三个标准。

这是我到目前为止所拥有的。我知道if(textbox...语句不起作用,但我找不到办法来做到这一点。 (使用"SELECT TOP 10 primaryFile FROM dbo.basket WHERE (basket.itemGuid = @itemguid) AND (basket.batchid = @batchid) AND (basket.account = @account"不会返回任何结果。

private List<string> GetSnippets()
{
    List<string> snippets = new List<string>();

    string connectionString = @"SNIP";
    //string sql = @"SELECT TOP 10 primaryFile FROM dbo.basket WHERE";
    string sql = @"SELECT TOP 10 primaryFile FROM dbo.basket WHERE (basket.itemGuid = @itemguid) AND (basket.batchid = @batchid) AND (basket.account = @account)";
    //if (textBoxGUID.Text.Length > 0) sql += " basket.itemGuid = @itemguid";
    //if (textBoxBatchID.Text.Length > 0) sql += " basket.batchid = @batchid";
    //if (textBoxAccount.Text.Length > 0) sql += " basket.account = @account";

    using (SqlConnection connection = new SqlConnection(connectionString))
    using (SqlCommand command = new SqlCommand(sql, connection))
    {
        command.Parameters.AddWithValue("@itemguid", textBoxGUID.Text);
        command.Parameters.AddWithValue("@batchid", textBoxBatchID.Text);
        command.Parameters.AddWithValue("@account", textBoxAccount.Text);

        try
        {
            connection.Open();
            if (connection.State == ConnectionState.Open)
            {
                using (SqlDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        snippets.Add((string)reader["primaryFile"]);
                        Console.WriteLine(reader["primaryFile"]);
                    }
                }
            }
        }
        catch (Exception)
        {
            throw;
        }
    }
    return snippets;
}

4 个答案:

答案 0 :(得分:4)

我会将where子句分开并根据需要附加到它。

string sql = @"SELECT TOP 10 primaryFile FROM dbo.basket ";
string where = "";
if (textBoxGUID.Text.Length > 0) 
{
    if(where.Length > 0 ) where += "AND "
    where += " (basket.itemGuid = @itemguid) ";
}
if (textBoxBatchID.Text.Length > 0) 
{
    if(where.Length > 0 ) where += "AND "
    where += " (basket.batchid = @batchid) ";
}
if (textBoxAccount.Text.Length > 0) 
{
    if(where.Length > 0 ) where += "AND "
    where += " (basket.account = @account) ";
}
if(where.Length > 0) {
    sql += "WHERE " + where;
}

然后你必须做同样的事情来为你的命令添加参数:

if (textBoxGUID.Text.Length > 0) command.Parameters.AddWithValue("@itemguid", textBoxGUID.Text);
if (textBoxBatchID.Text.Length > 0) command.Parameters.AddWithValue("@batchid", textBoxBatchID.Text);
if (textBoxAccount.Text.Length > 0) command.Parameters.AddWithValue("@account", textBoxAccount.Text);

代码更多,但它允许您准确搜索所提供的参数。

答案 1 :(得分:2)

如果我正确遵循,您希望根据所有填充变量过滤结果,以处理在变量为空(OR)时需要添加NULL的无填充变量:

WHERE (basket.itemGuid = @itemguid OR @itemguid IS NULL)
  AND (basket.batchid = @batchid OR @batchid IS NULL) 
  AND (basket.account = @account OR @account IS NULL)

答案 2 :(得分:1)

另一种方式,

@"SELECT TOP 10 primaryFile 
FROM dbo.basket 
WHERE 1 = case when ISNULL(@itemguid, basket.itemguid) = basket.itemGuid then 1 else 0 end
and   1 = case when ISNULL(@batchid, basket.batchid) = basket.batchid then 1 else 0 end
and   1 = case when ISNULL(@account, basket.account) = basket.account then 1 else 0 end"

答案 3 :(得分:1)

您可以使用:

string sql = @"SELECT TOP 10 primaryFile FROM dbo.basket WHERE 1 = 1";

if (textBoxGUID.Text.Length > 0) 
{
    sql += " AND basket.itemGuid = @itemguid";
}
if (textBoxBatchID.Text.Length > 0) 
{
    sql += " AND basket.batchid = @batchid";
}
if (textBoxAccount.Text.Length > 0) 
{
    sql += " AND basket.account = @account";
}