多个可选的WHERE参数

我想为一组SQL查询创建一个filter,以便用户可以最多三个值过滤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 GetSnippets() { List snippets = new List(); 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; } 

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

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

我会将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); 

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

你可以用这个:

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

其他方式,

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