There's several sql parser on
Google[
^].
If you can't use it, i'd suggest to:
1. create a list or dictionary with keywords to find
Dictionary<string, string> keywords = new Dictionary<string, string>();
keywords.Add("columns", "SELECT");
keywords.Add("tables", "FROM");
keywords.Add("where", "WHERE");
keywords.Add("group", "GROUP BY");
keywords.Add("order", "ORDER BY");
2. create a list which will hold the information about founded keywords
List<Tuple<string, string, int>> found = new List<Tuple<string, string, int>>();
string sComm = "your select statement here";
foreach(string kw in keywords.Values)
{
int start = 0;
int end = sComm.Length;
int at = 0;
while(start<end)
{
at = sComm.IndexOf(kw, start, StringComparison.InvariantCultureIgnoreCase);
if(at==-1) break;
found.Add(new Tuple<string, string, int>(keywords.FirstOrDefault(x=>x.Value==kw).Key, kw, at));
start=at+1;
}
}
found = found.OrderBy(x=>x.Item3).ToList();
Found list looks like:
columns SELECT 0
tables FROM 72
where WHERE 101
columns SELECT 132
tables FROM 164
where WHERE 193
group GROUP BY 326
order ORDER BY 356
Now, you have to implement the mechanism which will get the parts between keywords. I have not enough time to do that for you.
Tip:
- columns in first select are between characters of
sComm
: 0 and 71
- tables in first select are between characters: 72 and 100
- etc.
In case you want to remove keyword, you have to add the length of that keyword to the starting character ;)
Good luck!