基于可能/不可能的多个值在数据库中搜索 [英] searching in database based on multiple values which may/maynot be supllied
问题描述
我有五个文本框 - a,b,c,d,e和一个搜索按钮
现在用户可以在任何文本框中提供输入(a或b或c或abc或任何可能的组合)
有可能几个文本框空白。所以一个空的()字符串进入where子句。
我想要一个选择查询,它应该根据数据库搜索数据库中的数据用户输入的值,并忽略未输入的值
从* tbl_Results中选择* ...
什么应该在哪里之后()
提前感谢
检查代码下面
<前lang =cs> 私人 void PrepareSql()
{
string sqlstring = < span class =code-string> select * from tblResults,其中1 = 1;
if (!txta.equals( ))
sqlstring + = and column1 like'% + txta.text + %';
if (!txtb.equals( ))
sqlstring + = and column2 like'% + txtb.text + %';
if (!txtc.equals( ))
sqlstring + = and column4 like'% + txtc.text + %';
if (!txtd.equals( ))
sqlstring + = and column5 like'% + txtd.text + %';
}
谢谢
--RA
Hi Maverick,
你可以做一件事
string query = select * from tblResults;
string sql = string.Empty;
if (!txta.equals( ))
sql + = 其中column1类似'%' + txta 。 text + %';
if (!txtb。 equals( ))
if (sql.equals( ))
sql + = 其中column2类似于'%' + txtb。 text + %';
else
sql + = 和column2类似'% + txtb。 text + %';
if (!txtc.equals( ))
if (sql.equals( ))
sql + = 其中column3类似于'%' + txtc。 text + %';
else
sql + = 和column3类似'% + txtc。 text + %';
if (!txtd.equals( ))
if (sql.equals( ))
sql + = 其中column4类似'%' + txtd。 text + %';
else
sql + = 和columnd类似'%' + txtd。 text + %';
query + = sql;
然后在查询变量中运行查询
如果它解决了你的问题,请解决它
谢谢和问候
Suman Zalodiya
I have five text boxes - a,b,c,d,e abd a search button
Now the user can provide input in any of the text boxes (either in a or b or c or abc or any possible combinations)
there is a possibility of few textboxes being blank. So a empty ( "" ) string goes into the where clause.
I want a select query in such a way that it should search the data in database according to the values entered by the user, and ignore the the values not entered
select * from tbl_Results where ...
what should b there after where()
thanks in advance
Check the code Below
private void PrepareSql() { string sqlstring = "select * from tblResults where 1=1 "; if (!txta.equals("")) sqlstring += "and column1 like '% " + txta.text + "%' "; if (!txtb.equals("")) sqlstring += "and column2 like '% " + txtb.text + "%' "; if (!txtc.equals("")) sqlstring += "and column4 like '% " + txtc.text + "%' "; if (!txtd.equals("")) sqlstring += "and column5 like '% " + txtd.text + "%' "; }
Thanks
--RA
Hi Maverick,
You can do one thing
string query="select * from tblResults "; string sql=string.Empty; if(!txta.equals("")) sql+="where column1 like '% " + txta.text + "%' ";
if(!txtb.equals("")) if(sql.equals("")) sql+="where column2 like '% " + txtb.text + "%' "; else sql+="and column2 like '% " + txtb.text + "%' ";
if(!txtc.equals("")) if(sql.equals("")) sql+="where column3 like '% " + txtc.text + "%' "; else sql+="and column3 like '% " + txtc.text + "%' ";
if(!txtd.equals("")) if(sql.equals("")) sql+="where column4 like '% " + txtd.text + "%' "; else sql+="and columnd like '% " + txtd.text + "%' ";
query +=sql;
Then run the query in "query" variable
Please make it resolved if it resolves your issue
Thanks and Regards
Suman Zalodiya
这篇关于基于可能/不可能的多个值在数据库中搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!