how to put words into a variables and check if it matches the data in database
I am using MSSQL and C# to write a search engine. I got a bunch of stop words in the database. In my default.aspx page, I got a textbox and a开发者_如何学Go search button. Users can type in a whole sentence into the textbox.
I wrote the code for searching a single keyword but not a whole sentence. This is the function in default.aspx.cs
private void ImageSearch()
{
if (txtSearch.Text.Trim().Equals(""))
{
//when types nothing
txtSearch.Focus();
Warning.Text = "Please enter the keyword to search";
//display message
return;
}
else
{
int check = Process.CheckStopWord(txtSearch.Text.Trim());
//check if the keyword is stop word
if (check == 0)
{
txtSearch.Focus();
Warning.Text = "No stop word found";
}
else
{
txtSearch.Focus();
Warning.Text = "Found Stop word";
}
}
}
and for the checkstopword function
public static int CheckStopWord(string keyword)
{
string check = "0";
string query = "SELECT COUNT (stopword) FROM stopwordtb WHERE [stopword] like '" + keyword + "'";
//count how many stop word matches the keyword entered, return a string of a number
accessDB dbaccess = new accessDB();
DataSet ds = dbaccess.queryData(query);
DataTable dt = ds.Tables[0];
if (dt.Rows[0][0].ToString() == check)
{
return 0;
//keyword != stop word in stopwordtb
// begin search in image database - not implemented yet
}
else
{
return 1;
//keyword = stop word in stopwordtb
}
}
my problem is, what if the user type in a whole sentence?
For example, if he types in " The tree" in the textbox, but "The" is a stop word, so I will just ignore the "the" and just search the image database for "tree".
How can I put the sentence into variables and search each of them individually? or are there any faster way?
Hope somebody can give me some help. Thanks
You need something like string[] words = sentence.Split(' ');
then check each entry in the words array.
Before you search in the database, you may first split the sentence, and then check each word is a stop word using a loop (ex: while-loop),
Hope this helps.
I have a similar function in c#. It Will accept a string or sentence ex: INPUT: File access Mr Brown OUTPUT: "'File' and 'access' and 'Mr' and 'Brown'" Transleted to SQL: '"File" and "access" and "Mr" and "Brown"'
private static string formatStringToSQLForFullTEXTSEARCH(string subject)
{
string[] subArray = subject.Trim().Split(' ');
string newSubject = "";
newSubject = "";
if (subArray != null && subArray.Length > 0)
{
newSubject = "'" + subArray[0] + "'";
for (int i = 1; i < subArray.Length; i++)
{
newSubject += " and '" + subArray[i]+"'";
}
}
return newSubject;
}
Hope it helps somebody!
精彩评论