我正在尝试创建高级搜索查询,允许用户输入任意数量的过滤器。此过滤器包含不同的Asp控件,如下拉列表和复选框。 这是我使用的查询,问题是它需要输入所有过滤器才能工作,我需要它接受任何过滤器集合。
此查询只能使用一个按钮。 (请在修改我的代码后发布您的答案,以便更好地理解。
我稍后会对查询进行差异化处理。
public partial class Searchpage : System.Web.UI.Page
{
SqlConnection con = new SqlConnection();
string price1;
string price2;
string osx;
string checktwog;
string checkthreeg;
string checkfourg;
string phonetype;
string cam;
string ram;
string q;
protected void Page_Load(object sender, EventArgs e)
{
con.ConnectionString = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
con.Open();
try
{
price1 = Convert.ToString(Session["price1"]);
price2 = Convert.ToString(Session["price2"]);
osx = Convert.ToString(Session["osx"]);
checktwog = Convert.ToString(Session["checktwog"]);
checkthreeg = Convert.ToString(Session["checkthreeg"]);
checkfourg = Convert.ToString(Session["checkfourg"]);
phonetype = Convert.ToString(Session["phonetype"]);
cam = Convert.ToString(Session["cam"]);
ram = Convert.ToString(Session["ram"]);
q = "select * from legacy where price >= " + price1 + " and price <= " + price2 + " and os = '" + osx + "' and gprsedge = '" + checktwog + "' and threeg= '" + checkthreeg + "' and fourg='" + checkfourg + "' and touchscreen='" + phonetype + "' and camera='" + cam + "' and ram='" + ram +"'";
SqlDataSource1.SelectCommand = q;
SqlDataSource1.DataBind();
答案 0 :(得分:1)
你可以做类似的事情(未经过测试的代码)
List<string> conditions = new List<string>();
if (price1 != null)
{
cmd.Parameters.Add("@param1", price);
conditions.Add(" price >= @param1 ");
}
if (price2 != null)
{
cmd.Parameters.Add("@param2");
conditions.Add(" price <= @param2 ");
}
.....
StringBuilder sb = new StringBuilder();
sb.Append("select * from legacy ");
if(conditions.Count > 0)
{
sb.Append("WHERE ");
sb.Append(string.Join(" AND ", conditions));
}
答案 1 :(得分:1)
检查以下算法
var filters = new List<filterItem>();
filters.Add(new filterItem(field: "price", op: ">=", value: Session["price1"]));
filters.Add(new filterItem(field: "price", op: "<=", value: Session["price2"]));
filters.Add(new filterItem(field: "os", op: "=", value: Session["osx"]));
filters.Add(new filterItem(field: "gprsedge", op: "=", value: Session["checktwog"]));
filters.Add(new filterItem(field: "threeg", op: "=", value: Session["checkthreeg"]));
filters.Add(new filterItem(field: "fourg", op: "=", value: Session["checkfourg"]));
filters.Add(new filterItem(field: "touchscreen", op: "=", value: Session["phonetype"]));
filters.Add(new filterItem(field: "camera", op: ">=", value: Session["cam"]));
filters.Add(new filterItem(field: "ram", op: ">=", value: Session["ram"]));
var conditions = filters
.Where(f => f.Value != null)
.Select((filter, index) => {
var parameterName = string.Format("p{0}", index);// eg: p0, p1, ...
var parameterValue = Convert.ToString(filter.Value);
//Add parameter to SelectParameters
SqlDataSource1.SelectParameters.Add(parameterName, parameterValue);
//Construct filter for query, eg price >= @p0
var condition = string.Format("{0} {1} @{2}", filter.Field, filter.Operator, parameterName);
return condition;
});
var predicate = string.Empty;
if (conditions.Count() > 0) {
predicate = string.Format("WHERE {0}", string.Join(" AND ", conditions));
}
var query = "SELECT * FROM legacy";
var selectCommand = string.Join(" ", query, predicate).Trim();
SqlDataSource1.SelectCommand = selectCommand;
类型filterItem
定义为
class filterItem {
public filterItem(string field, string op, object value) {
this.Field = field;
this.Operator = op;
this.Value = value;
}
public string Field { get; private set; }
public string Operator { get; private set; }
public object Value { get; private set; }
}