Search Strings

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • charindal
    New Member
    • Dec 2009
    • 17

    Search Strings

    How can i search within a string say a description of items and would like the program to search for matching words in any order but they should relate e.g. consider this description: CEMENT AFRISAM 50KG SATCHETS....
    i would want a user to lets say type in the words "SATCHETS CEMENT", the search code should be able to list all descriptions that have SATCHETS AND CEMENT in one line.
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    Loops within loops checking all the suspect words against all the strings you want to search. Making use of the String.Contains method.

    Returns a value indicating whether a specified string occurs within this string, using the specified comparison rules.

    Comment

    • charindal
      New Member
      • Dec 2009
      • 17

      #3
      i m trying that but its not coming alright, i might be missing something do you have a sample code?

      Comment

      • Christian Binder
        Recognized Expert New Member
        • Jan 2008
        • 218

        #4
        Maybe you first show us what you've tried (code) and tell us the problem you're facing with your code. So I could help you finding the "something" you're missing.

        Comment

        • ThatThatGuy
          Recognized Expert Contributor
          • Jul 2009
          • 453

          #5
          you can use regular expressions for checking the existence of the word in a string
          with expression as ".*SATCHETS CEMENT*." which will search for the word in string and return whether the word exists....

          have a look at the tutorial below if you need any help
          http://msdn.microsoft.com/en-us/libr...95(VS.80).aspx is a way where you can search

          Comment

          • charindal
            New Member
            • Dec 2009
            • 17

            #6
            this was an error i have re entered it below.

            Comment

            • charindal
              New Member
              • Dec 2009
              • 17

              #7
              Code:
              char[] splitchar = { ' ' };
                          string[] str = txtstocksearch.Text.Split(splitchar);
                          foreach (string item in str)
                          {
                              SqlDataAdapter adp = new SqlDataAdapter("select * from stock where stock_description like '%" + item + "%'  , connection.connect());
                        
                
                          DataSet ds = new DataSet();
                          adp.Fill(ds);
                          DataTable dt = ds.Tables[0];
                          dataGridView2.RowHeadersVisible = false;
                          dataGridView2.DataSource = dt;
                          dataGridView2.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;
                          connection.connect ().Close();
              Last edited by tlhintoq; Mar 9 '10, 01:58 PM. Reason: [CODE] ...Your code goes between code tags [/CODE]

              Comment

              • tlhintoq
                Recognized Expert Specialist
                • Mar 2008
                • 3532

                #8
                TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.

                Comment

                • jkmyoung
                  Recognized Expert Top Contributor
                  • Mar 2006
                  • 2057

                  #9
                  SqlDataAdapter adp = new SqlDataAdapter( "select * from stock where stock_descripti on like '%" + item + "%' , connection.conn ect());




                  You want something like
                  select * from stock where
                  stock_descripti on like '%" + item + "%' AND
                  stock_descripti on like '%" + item2 + "%' AND
                  stock_descripti on like '%" + item3 + "%' ....
                  correct?

                  Create the query string beforehand. For each item beyond the first, add:

                  "AND \n stock_descripti on like '%" + item + "%'

                  Then do your query.

                  Comment

                  • charindal
                    New Member
                    • Dec 2009
                    • 17

                    #10
                    I seem to have found a lead. i enabled my fulltext indexing on SQL and the problem ha been greatly minimised. however ther is still one annoying problem. how do you pass wildcards from a textbox into the select statment. e.g when in sqladmin, it is easier to type a command like select * from a where contains(b,' "cem* " '), here it will list everything that starts with cem. however, when in c# how do i incorporate the wildcard * to my text box. if i use select * from a where contains(b,' " + textbox1.tex + " *') nuthing happends

                    Comment

                    • Christian Binder
                      Recognized Expert New Member
                      • Jan 2008
                      • 218

                      #11
                      Maybe you should use
                      Code:
                      string command = "SELECT * FROM a WHERE CONTAINS(b, ' [U]\"[/U]" + textBox1.Text+ "*[U]\"[/U] ')";
                      Notice the underlined \"

                      Comment

                      • charindal
                        New Member
                        • Dec 2009
                        • 17

                        #12
                        thank you very much. it seems to be behaving....

                        Comment

                        Working...