本文介绍了什么是错的查询?或code?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
一个用户在搜索charactre进入这样的*的蓝,黄绿色,粉红色,堆放超过流的*
现在我想把这个变成数组,然后使用以下code搜索它,
A user enter in the search charactre like this*"blue,yellow green,red pink,stack over flow"*Now i want to put this into array, and search it using the following code,
string[] search = mysearch.Split(',');
List<Result> myresult = new List<Result>();
for (int kk = 1; kk < search.Length; kk++)
{
where += " And '%" + search[kk] + "%'";
OleDbCommand sqlcmdCommand0 = new OleDbCommand("select Distinct name from table1 where search like '%" + search[0] + "%' " + where + " order by name", myCon);
sqlcmdCommand0.CommandType = CommandType.Text;
OleDbDataReader sda = sqlcmdCommand0.ExecuteReader();
while(sda.read())
{
myresult.name= sda.getString(0);
Result.add(myresult);
}
}
return Result;
查询是这样的:
select Distinct name from table1 where search like '%blue%' And '%yellow%' And '%Green %' order by name
和它应该是这样的:
select Distinct name from table1 where search like '%blue%' And '%yellow Green %' order by name
但问题是,它没有单独的逗号空间后弦,我希望把串入逗号的occurances后阵列的字符之间的空间后,没有。
but the problem is that it seperate the string after the space not at the comma, and i want to put the string into the array after the occurances of comma not after the space between the character.
推荐答案
OK,我想你可能只是想扔掉你的当前code ......这里就是你需要做的生成<$ C $什么C> WHERE 状态:
OK, I think you might just want to throw away your current code... Here's what you need to do to generate the WHERE
condition:
string[] searchTerms = mySearch.Split(',');
StringBuilder conditions = new StringBuilder();
foreach(string term in searchTerms)
{
if (conditions.Length > 0)
conditions.Append(" AND ");
conditions.AppendFormat("search LIKE '%{0}%'", term.Replace("'", "''"));
}
string query = "select Distinct name from table1";
if (conditions.Length > 0)
query = query + " WHERE " + conditions;
这篇关于什么是错的查询?或code?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!