locked
how do i read data from my database and do a wordcount RRS feed

  • Question

  • hi people, 
    i am currently doing database C# using visual C sharp express and i am really new towards database programming. I actually taken my database from mc access using oledbconnection.

    One of the column in my database contain many words seperated by a comma in each particular row. I am currently using oledbdatareader to read my database but i realise i only can read the whole column at a time instead of row by row.

    My table look like this

    object        description words
    ball            round, big, orange
    pencil         long, blue

    something like that and i have to count the number of description words per row.

    I have heard about using comma delimited string and putting it into array but i do not know how to do about it. Can anyone teach me how to obtain the words from my database and count the number of words i have per row after connection? Thanks in advance.
    Friday, June 12, 2009 7:35 AM

Answers

  • Hi,

    Please try to use ss[i].Contains(a);

    Harry
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by clueless123 Tuesday, June 16, 2009 8:13 AM
    Tuesday, June 16, 2009 5:32 AM
  • Hi,

    Please take a look at the code snippet bellow:
        
        while(reader.Read())  
            {  
              Console.WriteLine(reader.GetValue(1).ToString() + " = " + reader.GetValue(3).ToString());
              string s=reader.GetValue(3).ToString();       
              string[] ss=s.Split(new char[]{','},StringSplitOptions.RemoveEmptyEntries);
              Console.WriteLine("count: "+ss.Length);          
            }

    Harry
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by clueless123 Tuesday, June 16, 2009 4:01 AM
    Tuesday, June 16, 2009 2:24 AM

All replies

  • Dear,
    After retrieving each record in a string, you can use the following piece of code to put the the string in an array. After which you can use each word in the comma list by Array Index.

    string str = dr[0].getString(); //The string Eg. round, big, orange
    string[] expr = str .Split(new char[] { ',' }); //The array expr will be created having indexes from 0 to 2

    I hope it helps.
    Thank You.
    Friday, June 12, 2009 8:12 AM
  • Thanks for the solution but i have one problem. As i am very new in doing this I seriously have no idea after declaring where should i put it or how to apply it. I really need your guidance.

    The code that i used is

    1. private static OleDbConnection con;
    2.  
    3. con = new OleDbConnection(@"Provider=Microsoft.JET.OLEDB.4.0;" + @"data source=Assignment.mdb");.
    4.  
    5. con.Open(); //connection must be openned
    6.  
    7. OleDbCommand cmd = new OleDbCommand("SELECT * from main", con);
    8.  
    9. OleDbDataReader reader = cmd.ExecuteReader();
    10.  
    11. while(reader.Read())
    12. {
    13.  
    14. Console.WriteLine(reader.GetValue(1).ToString() + " = " + reader.GetValue(3).ToString()); // From here i am only able to get the whole column from 1 and 3 whereby column 3 is my list of words
    15. }


    So currently i am very clueless in what to do next and kind of stuck. Thanks for helping (:
    Saturday, June 13, 2009 7:43 AM
  • Hi,

    Please take a look at the code snippet bellow:
        
        while(reader.Read())  
            {  
              Console.WriteLine(reader.GetValue(1).ToString() + " = " + reader.GetValue(3).ToString());
              string s=reader.GetValue(3).ToString();       
              string[] ss=s.Split(new char[]{','},StringSplitOptions.RemoveEmptyEntries);
              Console.WriteLine("count: "+ss.Length);          
            }

    Harry
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by clueless123 Tuesday, June 16, 2009 4:01 AM
    Tuesday, June 16, 2009 2:24 AM
  • hi .. thanks for the codes it words perfectly but now i have any problem which i need help in.

    my description of words per object might have more than one word which are the same becuase it is key in by different user.

    so for now i would only want to count the words the user search for .
    Example. object        description words
    ball                     round, big, orange, big
    pencil                  long, blue, big, long

    so if user search for big using access queries, it will count the number of big under ball and the number of big under pencil

    currently what i did was

    for(i=0; i<ss.Length; i++)
    {
          if(ss[i].Equals(searchtext.text))
         {
                string wordcount = (ss.Length).Tostring();
                console.WriteLine(wordcount);
         }
    }


    but on my output the words will just run without showing any counts. sorry if is abit messy. Thanks fro the help
    Tuesday, June 16, 2009 4:09 AM
  • Hi,

    Please have a try:

                 Console.WriteLine(reader.GetValue(1).ToString() + " = " + reader.GetValue(3).ToString());
                  string s=reader.GetValue(3).ToString();       
                  string[] ss=s.Split(new char[]{','},StringSplitOptions.RemoveEmptyEntries);
               
                  int count = 0;
                  for (i = 0; i < ss.Length; i++)
                  {
                      if (ss[i].Equals(searchtext.text))
                      {
                          count++;
                         // string wordcount = (ss.Length).Tostring();
                         // console.WriteLine(wordcount);
                      }
                  }
                 Console.WriteLine("count : "+count);

    Harry
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Tuesday, June 16, 2009 4:18 AM
  • Thanks. but i have a couple of problem and questions.

    Firstly, when i put in the code , the output only show one count in the first object and the rest are all 0 despite them also having the word that i search.

    secondly, it only shows one count when i have more than one of the words.

    and i still have a couple of helps needed, example when i search for big only, i would want bigger to add into the count also. Is it possible?

    And actually all my search textbox are all in access whereby i use queries so am i able to do a search in access and c sharp helping me to count the number of the words i search ? or must i create a textbox in c sharp form to do it.
    Tuesday, June 16, 2009 4:59 AM
  • Hi,

    Could you post more code you are working with ? and please post some data in the table .

    Harry
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Tuesday, June 16, 2009 5:02 AM
  • i used the codes you gave me for the first try but there is no count.

    Example. object        description words
    ball                     round, big, orange, big
    pencil                  long, blue, big, long
    ambulance           alert, emergency, high emergency, alert emergency
    phone                  incoming message, message alert, high alert, message incoming, emergency, alert


    my table consist of all this and more.
    so if i were to search for alert.
    not only alert will be counted as one count, alert emergency also counted as another count. so there will be 2 count under ambulance and 3 counts under phone.

    so for now i am using sql which is 

      int count = 0;
     string a = select * from main where (((main.keyword) Like searchtext)); // this is taken from access sql view
                  for (i = 0; i < ss.Length; i++)
                  {
                      if (ss[i].Equals(a))
                      {
                          count++;
                         // string wordcount = (ss.Length).Tostring();
                         // console.WriteLine(wordcount);
                      }
                  }
                 Console.WriteLine("count : "+count);

    is the way i do it correct?

    Tuesday, June 16, 2009 5:24 AM
  • Hi,

    Please try to use ss[i].Contains(a);

    Harry
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by clueless123 Tuesday, June 16, 2009 8:13 AM
    Tuesday, June 16, 2009 5:32 AM
  • Thanks it works!

    Is there anyway to put the count into one more column and back into access?
    Tuesday, June 16, 2009 7:31 AM
  • Hi,

    If there is a column there , you might want to record the row number and update it with the count later.

    Harry
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Tuesday, June 16, 2009 7:34 AM
  • ermm sorry i am really very new in doing this so i have no idea at all how to do it . you mean i can use sql to put it back into access?

    Tuesday, June 16, 2009 7:38 AM
  • Hi,

    I found a article talking about how to manupulate access database, please have a look at it and see if it can help you to do the job.
    http://www.codeproject.com/KB/database/simpledbreadwrite.aspx

    Harry
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Tuesday, June 16, 2009 7:52 AM
  • hi, sorry i am unable to solve this. My search textbox is in access and thus i would want to return the count of my search words (which is keyed into from access) back into access with a new column. So am i suppose to create a new column in access ? or there is a code to do it ?
    Tuesday, June 16, 2009 8:54 AM
  • Hi,

    I have no idea how to insert a column using code. Why not adding the column manually when the table is created as it will be used to store the count.

    Could you please be more specific about " search textbox is in access " , the textbox is not a TextBox control on winform?

    Harry

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Tuesday, June 16, 2009 9:00 AM
  • ACtually it works like this.

    In access database, i created a textbox which allow user to key in and i will provide the user with a new table created to contain objects that contain the description the user search for but now i need to do a wordcount so i used C# to read the new table created from access. So in order to count the number of words which user search i have to create a textbox again in C sharp and user have to search the same word again in C sharp in order to do the word count.

    My question will be is there a shorter way whereby user need not key in two time to get the object and wordcount but only once and both will be able to work at the same time
    Tuesday, June 16, 2009 9:15 AM
  • Hi,

    I'm still not quite sure I have understood it.
    Could you please post the code relating to this?

    As far as I can see , you might want to store the data into a dataset , and manupulate it using c#.
    Is there any reason that a new table is needed ? Every time a user search a key word , a new table will be created?

    Harry

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Tuesday, June 16, 2009 9:23 AM
  • okay i dono how should i put it.

    In access, i created a form which include a textbox for user to search the words.
    so under access i created a make table query and include the code "select * into search from main where(((descriptionwords)Like searchtxt))"

    search is my new table and searchtxt is my textbox created in access for user input.


    so in C#, i connect the table and select * from search. So now i want to count the number of words the user search in access.

    under if(ss[i].contained(_____)

    What should i compare with? because currently i am creating one more textbox in c# and call in searchtext and thus user have to search again in C# to obtain the wordcoutn.
    Tuesday, June 16, 2009 9:31 AM
  • Hi,

    I'd recommend implement this on the winform instead of creating form and textbox on access.

    So a user just need to input the search keyword one time on the textbox of winform.

    Harry
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Tuesday, June 16, 2009 10:12 AM
  • okayy thanks. but how do i eliminate those object that does not contain the words that i search. can i use if else to compare whether object contain the word ?
    Wednesday, June 17, 2009 1:31 AM
  • Hi,

    What are these objects ? rows in database?

    Harry
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Wednesday, June 17, 2009 2:12 AM
  • hi.. ya this are my different objects in database. So when i search the keyword i only want the object that contain the search word to appear. so i though of creating a data grid view and display my result after search together with the word count . so do you think is possible? and how am i going to do about it. Thanks
    Wednesday, June 17, 2009 3:54 AM