如何在Checklistbox的基础上编写conditon进行搜索 [英] how to write conditon for searching on the base of Checklistbox
问题描述
在db表的列中不同的类别名称
例如,
类别
惠普
Lenova
戴尔
APPLE
现在开启我在网页表单中为多个类别名称放置了复选框列表。搜索基于不同的类别(上面提到)我正在使用
inside of a db table's column different names of categories
e.g.,
Category
HP
Lenova
DELL
APPLE
now on a web form i have placed a check boxes list for multiple category names. Search is base on different categories(above mentioned) i am using
if else
但问题是如何编写条件或Condtion 进行多次搜索?
i我这样做
but the problem is how to write the conditions or Condtion for multiple search ??
i am doing like this
if(Checkboxeslist.selectedIndex==0)//for hp
{
sql query
}
if(Checkboxeslist.selectedIndex==1)//for Dell
{
sql query
}
if(Checkboxeslist.selectedIndex==1 && Checkboxeslist.selectedIndex==0)//for Dell
{
sql query
}
等......条件永远不会结束我的意思是太多条件。
请帮我解决这个困境
and so on...conditions will never end i mean too much conditions.
please help me how to resolve this dilemma
推荐答案
为什么不尝试这个
遍历所选项目并形成动态查询,最后执行它以获得所需的输出。
Why dont you try this
Loop through the selected item and form a dynamic query and finally execute it to get your desired output.
string SQLQuery = "select blah, blah from Category where 1=1"
foreach (var item in checkedListBox1.CheckedItems)
{
SQLQuery = SQLQuery + " or CategoryName= " + item
}
//Execute your query
希望有帮助
Hope it helps
您好,
首先获取所有选中的列表复选框值。
Hi,
First of all get list of all selected checkbox values.
string CategoryName = string.Empty;
string query = "select * from Category";
foreach(ListItem item in checkedListBox1.Items)
{
if(item.Selected)
{
CategoryName += item.Text + ",";
}
}
if (!string.IsNullOrEmpty(CategoryName))
{
//remove last ,
CategoryName = CategoryName.Remove(CategoryName.Length - 1, 1);
// now complete your query
query += "where CategoryName in (" + CategoryName + ")";
// now you sqlexecute code
}
您可以尝试SQL查询,例如
You can try in SQL query like
Select * from Category where PK_CATID is not null (Which is always true)
现在连接这个Checkboxeslist.selectedValue
Now concatenate with this Checkboxeslist.selectedValue
sql="Select * from Category where PK_CATID is not null"
sql=sql+ and CategoryName='+Checkboxeslist.selectedValue
这篇关于如何在Checklistbox的基础上编写conditon进行搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!