[英]Advance search filter using ASP.NET C#
我正在嘗試創建高級搜索查詢,該查詢允許用戶輸入所需的任意數量的過濾器。 該過濾器包含不同的Asp控件,例如dropdownlist和checkbox。 這是我使用的查詢,問題是它需要輸入所有過濾器才能工作,我需要它接受任何過濾器集合。
此查詢僅工作一個按鈕。 (請在修改我的代碼后發布您的答案,以便更好地理解。
稍后我將對查詢進行參數化。
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();
你可以做類似的事情(代碼未經測試)
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));
}
檢查以下算法
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; }
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.