解析SQL查询并拉出列名和表名

解析SQL查询并拉出列名和表名

问题描述:

我有一个这样的查询脚本:

I have a Query Script like this:

从 dbo.View1 中选择 View1.OrderDate,View1.Email,SUM(View1.TotalPayments)WHERE (View1.OrderStatus = 'Completed')GROUP BY View1.OrderDate,View1.Email有(SUM(View1.TotalPayments) > 75);

SELECT View1.OrderDate,View1.Email,SUM(View1.TotalPayments) FROM dbo.View1 WHERE (View1.OrderStatus = 'Completed') GROUP BY View1.OrderDate,View1.Email HAVING (SUM(View1.TotalPayments) > 75);

有什么方法可以从 SQL 查询中提取一些关键信息?例如表名和列名,我有两个问题:

Is there any approach that we can pull some key information out from SQL query? such as table name and column name ,I have 2 question:

  1. 我确实搜索了一些解析器,例如 ANTLR ,但我找不到查找解释在 C# 语言中使用此解析器的文档.
  2. 有什么方法可以使用实体框架来解析 sql 查询?我的查询是完全动态的,它们是在运行时创建的

我认为最好的答案是使用 Irony 解析器:http://irony.codeplex.com/

I think the best answer is going to be to use the Irony parser: http://irony.codeplex.com/

Hanselman 有一个关于如何使用它来解析 SQL 的很好的链接:http://www.hanselman.com/blog/TheWeeklySourceCode59AnOpenSourceTreasureIronyNETLanguageImplementation

Hanselman has a great link to how to use it to parse SQL: http://www.hanselman.com/blog/TheWeeklySourceCode59AnOpenSourceTreasureIronyNETLanguageImplementationKit.aspx

我希望这会有所帮助,祝你好运!

I hope this helps, and best of luck!