Parsing SQL code in C# [closed]

2019-01-01 12:53发布

I want to parse SQL code using C#.

Specifically, is there any freely available parser which can parse SQL code and generate a tree or any other structure out of it? It should also generate the proper tree for nested structures.

It should also return which kind of statement the node of this tree represents.

For example, if the node contains a loop condition then it should return that this is a "loop type" of a node.

Or is there any way by which I can parse the code in C# and generate a tree of the type I want?

标签: c# sql
8条回答
呛了眼睛熬了心
2楼-- · 2019-01-01 13:17

Scott Hanselman recently featured the Irony project which includes a sample SQL parser.

查看更多
旧人旧事旧时光
3楼-- · 2019-01-01 13:21

Try GOLD Parser, it's a powerful and easy to learn BNF engine. You can search the grammars already made for what you want (ie: SQL ANSI 89 Grammar).

I started using this for HQL parsing (the NHibernate query language, very similar to SQL), and it's awesome.

UPDATE: Now the NH dev team has done the HQL parsing using ANTLR (which is harder to use, but more powerful AFAIK).

查看更多
像晚风撩人
4楼-- · 2019-01-01 13:24

Try ANTLR - There are a bunch of SQL grammars on there.

查看更多
一个人的天荒地老
5楼-- · 2019-01-01 13:30

As Diego suggested, grammars are the way to go IMHO. I've tried Coco/r before, but that is too simple for complex SQL. There's ANTLR with a number of grammars ready.

Someone even tried to build a SQL engine, check the code if there's something for you in SharpHSQL - An SQL engine written in C#.

查看更多
流年柔荑漫光年
6楼-- · 2019-01-01 13:31

You may take a look at a commerical component: general sql parser at http://www.sqlparser.com It supports SQL syntax of Oracle, T-SQL, DB2 and MySQL.

查看更多
刘海飞了
7楼-- · 2019-01-01 13:32

VSTS 2008 Database Edition GDR includes assemblies that handle SQL parsing and script generation that you can reference from your project. Database Edition uses the parser to parse the script files to represent in-memory model of your database and then uses the script generator to generate SQL scripts from the model. I think there are just two assemblies you need to have and reference in your project. If you don't have the database edition, you may install the trial version to get the assemblies or there might be another way to have them without installing the database edition. Check out the following link. Data Dude:Getting to the Crown Jewels .

查看更多
登录 后发表回答