Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Alternatives to DataTable Searches?

Alternatives to DataTable Searches?

Scheduled Pinned Locked Moved C#
databasewinformsperformancehelpquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Paul Gates
    wrote on last edited by
    #1

    Hi All, I have a DataTable in a Windows Forms project that I would like to use a Select statement. The problem is, I'd like to be able to do wildcard searches (including single character searches). The DataTable only supports multiple-character searches (*), and only at the beginning or end of the search criteria. What are my alternatives? I'd rather not have to go back to the database, unless that is the best way. The DataTable was a performance enhancement. Thanks, pagates

    W 1 Reply Last reply
    0
    • P Paul Gates

      Hi All, I have a DataTable in a Windows Forms project that I would like to use a Select statement. The problem is, I'd like to be able to do wildcard searches (including single character searches). The DataTable only supports multiple-character searches (*), and only at the beginning or end of the search criteria. What are my alternatives? I'd rather not have to go back to the database, unless that is the best way. The DataTable was a performance enhancement. Thanks, pagates

      W Offline
      W Offline
      Wjousts
      wrote on last edited by
      #2

      I encounted a similar problem. It's quite frustrating that searching in a datatable isn't more flexible. I found a partial solution by parsing the search string to replace wildcards in the middle of the search string like this:

      private string ParseWildCards(string strWild)
      {
      	// Look for wildcards that DO NOT occur at the start or end of the string
      	Regex reg = new Regex(@"(?<=.)\*(?=.)");
      	if (reg.IsMatch(strWild))
      	{
      		// We have a wildcard in the middle of the expression - check this is actually a LIKE - else it won't work
      		int i = strWild.IndexOf("LIKE");
      		if (i > 0)		
      		{
      			string col = strWild.Substring(0,i).Trim();	// get the column name
      			string strReplace = "*' AND " + col + " LIKE '*";
      			return reg.Replace(strWild,strReplace);
      		}
      	}
      	return strWild;
      }
      

      This will replace a string like "col like 'aaa*bbb'" with "col like 'aaa*' and col like *bbb' which kind of works except that there is no way to stop it matching a string like '...bbbaaa...' because you can't specify that it should match the 'aaa*' part before the '*bbb' part. If anybody has a better solution I would love to hear about it.

      P 1 Reply Last reply
      0
      • W Wjousts

        I encounted a similar problem. It's quite frustrating that searching in a datatable isn't more flexible. I found a partial solution by parsing the search string to replace wildcards in the middle of the search string like this:

        private string ParseWildCards(string strWild)
        {
        	// Look for wildcards that DO NOT occur at the start or end of the string
        	Regex reg = new Regex(@"(?<=.)\*(?=.)");
        	if (reg.IsMatch(strWild))
        	{
        		// We have a wildcard in the middle of the expression - check this is actually a LIKE - else it won't work
        		int i = strWild.IndexOf("LIKE");
        		if (i > 0)		
        		{
        			string col = strWild.Substring(0,i).Trim();	// get the column name
        			string strReplace = "*' AND " + col + " LIKE '*";
        			return reg.Replace(strWild,strReplace);
        		}
        	}
        	return strWild;
        }
        

        This will replace a string like "col like 'aaa*bbb'" with "col like 'aaa*' and col like *bbb' which kind of works except that there is no way to stop it matching a string like '...bbbaaa...' because you can't specify that it should match the 'aaa*' part before the '*bbb' part. If anybody has a better solution I would love to hear about it.

        P Offline
        P Offline
        Paul Gates
        wrote on last edited by
        #3

        Thanks for the reply. I will combine this with an answer I saw in the MSDN newsgroups. Basically, the MSDN answer was to manually go through the rows and doing a RegEx pattern search. While that could be slow with tens of thousands of rows, if I implement your solution first, I will at least have some rows filtered out. Again, if anybody has a better idea, I'm all ears. Thanks again, PAGates

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups