SQL Parser in C

SQL Parsing in C#?

Irony for C# has an LALR parser with several grammars already created, including one for SQL (as you can see from the screenshot on the home page). It's a very easy to use parser using operator overloads to define the grammars in a way reminiscent to BNF.

Parsing SQL code in C#

[Warning: answer may no longer apply as of 2021]

Use Microsoft Entity Framework (EF).

It has a "Entity SQL" parser which builds an expression tree,

using System.Data.EntityClient;
...
EntityConnection conn = new EntityConnection(myContext.Connection.ConnectionString);
conn.Open();
EntityCommand cmd = conn.CreateCommand();
cmd.CommandText = @"Select t.MyValue From MyEntities.MyTable As t";
var queryExpression = cmd.Expression;
....
conn.Close();

Or something like that, check it out on MSDN.

And it's all on Ballmers tick :-)

There is also one on The Code Project, SQL Parser.

Good luck.

How parse SQL to Datatable or array in c#?

var str = "select 'a1,a2' f1, 2 f2;";
Regex.Matches(str, "(^|[\\s,\\,]){1}(\\'.*\\'\\s*\\w+|[^\\,^']*)[\\,,$,;]")
.Cast<Match>()
.Select(m => m.Value.Split(new[] { ' '}, StringSplitOptions.RemoveEmptyEntries)
.First().Replace("\'",""))
.ToArray();

How to parse AND/OR input in c#?

You will probably need to parse the input yourself, but with a parser generator such as ANTLR, it is not too much work.



Related Topics



Leave a reply



Submit