c# - Loop through a list of words/phrases, match with database table rows -
i have list of words/phrases in .txt (each word/phrase in new line), want loop through each word/phrase , check whether these words/phrases appear in 6th column of comments table. cannot work out below code, nil appear in datagridview despite having matched "keywords", can review/correct code? give thanks you.
private void button_search1_click(object sender, eventargs e) { datatable flaggedcomments = new datatable("flaggedcomments"); using (mysqlconnection sqlconn = new mysqlconnection(strprovider)) { using (mysqldataadapter da = new mysqldataadapter("select comment_id, comments_date, comments_time, author, title, comments_comment comments order comments_date asc, comments_time asc", sqlconn)) { da.fill(flaggedcomments); } } string[] words = file.readalllines(sourcedirtemp + "a_list_of_words_and_phrases.txt"); foreach (datarow da in flaggedcomments.rows) { string itemcomments = da[5].tostring(); if (words.any(wordorphrase => regex.ismatch(itemcomments, @"\b" + regex.escape(wordorphrase) + @"\b", regexoptions.ignorecase))) { datagridview_flaggedcomments.rows.add(da); string itemtitle = da[4].tostring(); string itemdate = da[1].tostring().replace(" 12:00:00 am", ""); string itemtime = da[2].tostring(); string itemauthor = da[3].tostring(); string itemcommentid = da[0].tostring(); richtextbox_flaggedcomments.appendtext("date: " + itemdate + "\ntime: " + itemtime + "\ncommenter: " + itemauthor + "\ntitle: " + itemtitle + "\ndescription: " + itemcomments + "\ncomment id: " + itemcommentid + "\n\n--------\n\n"); } } } i thought rows.add(da) line work, when clicked button, gives me error saying no row can added datagridview command not have columns.
sample "comments_comment" e.g.
aberdeen asset management release trading update 2 months 28 feb next regulatory approval acquisition of scottish widows investment partnership.analysts' expectations: 'for 2 months expect assets under management of £188 billion downwards 3 per cent first quarter.
how using databinding, instead of trying add together rows gridview. can replace foreach in code this:
var query = flaggedcomments.asenumerable().where(r => words.any(wordorphrase => regex.ismatch(r.field<string>("comments_comment"), @"\b" + regex.escape(wordorphrase) + @"\b", regexoptions.ignorecase))); datagridview_flaggedcomments.datasource = query.asdataview(); you need have reference system.data.datasetextensions.dll in visual studio project work.
c# mysql regex datagridview
No comments:
Post a Comment