本文介绍了将预定义符号后的单词拆分为多个搜索查询&amp;术语的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我正在开发一个基于C#和SQL Server CE 3.5的简单翻译应用程序 我有一个搜索文本框,通过`textBox1搜索数据库中的某些列.Text`正常的SQL查询[`SELECT .. LIKE'%%'`] ------- --- *我想达到的目标:* **我想在某些符号后搜索所有单词(+例如)在数据库的所有位置,所以它们不需要在完整的上下文中编写**(数据库中存在的单词一字一行) *换句话说:* **我想在某些符号后分割单词,以便程序独立地搜索每个单词**(在符号前搜索单词,在符号后单独搜索每个单词) b $ b ---------- ***示例:*** 如果我试图搜索举证责任的值,我将在之前的上下文中写出来,但是对于用户,这将不适用。所以我希望他在他愿意寻找的两个词之间放一个符号(即他应该搜索负担+证据) 图片1 :http://i.imgur.com/sd5Y5B7.jpg,图2:http://i.imgur.com/gVj41xP.jpg ---------- 编辑 - 我的搜索按钮代码: sqlcmd = new SqlCeCommand (SELECT * FROM T1 WHERE EnglishWord like %+ textBox1.Text +%'或EnglishDesc,如'%'+ textBox1.Text +%'或ArabicWord like'%+ textBox1.Text +%'OR ArabicDesc like'%+ textBox1.Text + %',sqlcon); 尝试 { listView1.Items.Clear(); sqldr = sqlcmd.ExecuteReader(); while(sqldr.Read()) { ListViewItem item = new ListViewItem(sqldr [ID]。ToString()); item.SubItems.Add(sqldr [EnglishWord]。ToString()); item.SubItems.Add(sqldr [EnglishDesc]。ToString()); item.SubItems.Add (sqldr [ArabicDesc]。ToString()); item.SubItems.Add(sqldr [ArabicWord]。ToString()); item.SubItems .Add(sqldr [Subject]。ToString()); listView1.Items.Add(item); } listView1.Enabled = true; label7.Text = listView1.Items.Count.ToString(); } catch(SqlCeException ex) { MessageBox.Sh ow(错误:+ ex.Message,有问题); } I'm working on a simple translation application based on C# & SQL Server CE 3.5I have a search textbox that searches certain columns in database through `textBox1.Text` with normal SQL query [`SELECT.. LIKE '% %'`] ----------*What I want to achieve :***I want to search for all the words after certain symbols (+ for example) in all locations in database , so they don't need to be written in the full context** (word after word as they exist in database)*In other words :***I want to split words after certain symbols , so that the program search for each word independently** (search the word before symbol and each word after symbol separately)----------***Example:***If I tried to search for the value "burden of proof" , I've to write it in the previous context, but for the user this will not apply. So I want him to put a symbol in-between the two words he is willing to search for (namely he should search for "burden+proof")Picture 1 : http://i.imgur.com/sd5Y5B7.jpg , Picture 2 : http://i.imgur.com/gVj41xP.jpg---------- Edit - my search button code : sqlcmd = new SqlCeCommand ("SELECT * FROM T1 WHERE EnglishWord like '%" + textBox1.Text + "%' OR EnglishDesc like '%" + textBox1.Text + "%' OR ArabicWord like '%" + textBox1.Text + "%' OR ArabicDesc like '%" + textBox1.Text + "%' ", sqlcon); try { listView1.Items.Clear(); sqldr = sqlcmd.ExecuteReader(); while (sqldr.Read()) { ListViewItem item = new ListViewItem(sqldr["ID"].ToString()); item.SubItems.Add(sqldr["EnglishWord"].ToString()); item.SubItems.Add(sqldr["EnglishDesc"].ToString()); item.SubItems.Add(sqldr["ArabicDesc"].ToString()); item.SubItems.Add(sqldr["ArabicWord"].ToString()); item.SubItems.Add(sqldr["Subject"].ToString()); listView1.Items.Add(item); } listView1.Enabled = true; label7.Text = listView1.Items.Count.ToString(); } catch (SqlCeException ex) { MessageBox.Show("Error: " + ex.Message, "Something wrong"); }推荐答案 假设您要查找所有记录每个单词出现在四列中的至少一列中,这样的东西应该起作用: Assuming you want to find all records where each word appears in at least one of the four columns, something like this should work:sqlcmd = sqlcon.CreateCommand();string[] words = textBox1.Text.Split(new[] { '+' }, StringSplitOptions.RemoveEmptyEntries);StringBuilder query = new StringBuilder("SELECT * FROM T1 WHERE 1 = 1");for (int index = 0; index < words.Length; index++){ string wordToFind = words[index]; string parameterName = "@word" + index; sqlcmd.Parameters.AddWithValue(parameterName, "%" + wordToFind + "%"); query.AppendFormat(" AND (EnglishWord Like {0} OR EnglishDesc Like {0} OR ArabicWord Like {0} OR ArabicDesc Like {0})", parameterName);}sqlcmd.CommandText = query.ToString(); 这也将修复 SQL注入 [ ^ ]漏洞。 如果用户搜索负担+证明,则生成的查询将如下所示: This will also fix the SQL Injection[^] vulnerability in your code.If the user searches for burden+proof, the resulting query will look something like this:SELECT *FROM T1WHERE 1 = 1And ( EnglishWord Like @word0 Or EnglishDesc Like @word0 Or ArabicWord Like @word0 Or ArabicDesc Like @word0 )And ( EnglishWord Like @word1 Or EnglishDesc Like @word1 Or ArabicWord Like @word1 Or ArabicDesc Like @word1 )/*Parameters:- @word0 = '%burden%'- @word1 = '%proof%'*/ 请阅读我的评论问题。 我建议阅读这些: 1)在SQL IN子句中使用逗号分隔值参数字符串 [ ^ ] 2 )将列中逗号分隔的数据转换为行以供选择 [ ^ ] Please, read my comment to the question.I'd suggest to read these:1) Using comma separated value parameter strings in SQL IN clauses[^]2) Converting comma separated data in a column to rows for selection[^] 这篇关于将预定义符号后的单词拆分为多个搜索查询&amp;术语的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
11-01 21:48