将WHERE 1 = 1
添加到基本查询,然后将AND
添加到每个选定的条件。
肯定使用SqlParameter
将值传递给sql查询。
public class Condition
{
public Func<bool> IsSelected { get; set; }
public string Text { get; set; }
public SqlParameter Value { get; set; }
}
var conditions = new[]
{
new Condition
{
IsSelected =() => checkBoxDate.Checked,
Text = "CreateDate >= @CreatedDate",
Value = new SqlParameter("@CreatedDate", new DateTime(2017, 8, 1))
},
new Condition
{
IsSelected =() => checkBoxUser.Checked,
Text = "User = @User",
Value = new SqlParameter("@User", "John")
},
new Condition
{
IsSelected =() => checkBoxNumber.Checked,
Text = "Number = @Number",
Value = new SqlParameter("@Number", 789)
},
new Condition
{
IsSelected =() => checkBoxId.Checked,
Text = "Id = @Id",
Value = new SqlParameter("@Id", 12)
}
}
var selectedConditions = conditions.Where(condition => condition.IsSelected()).ToList();
var baseQuery = "SELECT Id, Number, User, CreatedDate FROM MyTable WHERE 1 = 1";
var parameters = selectedConditions.Select(condition => condition.Value);
var commandText =
selectedConditions.Aggregate(new StringBuilder(),
(text, condition) =>
{
text.Append(" AND ");
text.Append(condition.Text);
return text;
},
(text) =>
{
text.Insert(0, baseQuery);
return text.ToString();
});
using (var connnection = new SqlConnection(connectionString))
using (var command = new SqlCommand(commandText, connection))
{
command.Parameters.AddRange(parameters);
connection.Open();
// execute command
}
由于@jmoreno建议您不要将WHERE 1 = 1
添加到查询中。然后SQL建设将看起来像下面的代码 - 选择你认为更适合你的需求的方法。
var baseQuery = "SELECT Id, Number, User, CreatedDate FROM MyTable";
var selectedText = selectedConditions.Select(condition => condition.Text);
var commandText =
selectedConditions.Any()
? $"{baseQuery} WHERE {string.Join(" AND ", selectedText)}"
: baseQuery;
有什么这么棘手的呢? SELECT * From Table_Name WHERE Column1 ='“&variable&”'AND ......我假设你知道你的用户会输入多少个变量。如果不是仅仅使用一堆If ElseIF语句来构建它。 –
取决于用户选择的内容,除了很多其他之外还有其他内容吗? – Dino
不是,您必须检查每个条件,看看是否需要添加到查询中。 –