我如何找出这个语句产生的实际SQL? [英] How do I find out the actual SQL that this statement generates?

查看:134
本文介绍了我如何找出这个语句产生的实际SQL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用VS2010,.NET4和EF4。我想看到在运行这个时生成的实际的SQL。此外,这是写这个声明的最好方法是什么?



这是我的代码:

  var cklContactItems = from a in dbTestCenterViews.appvuChecklistExports 
其中a.MarketChecklistID == MCLID
&&& a.checklistSectionID == SID
&&& a.fieldGroupOrder!= null
orderby a.fieldGroupOrder升序
选择新的{a.Column1,a.Column2,a.Column3,a.Column4,a.Column5,a.Column1FieldID,a.Column2FieldID, a.Column3FieldID,a.Column4FieldID,a.Column5FieldID,a.fieldGroupOrderLabel};


解决方案

  var query =(from x in context.MyEntity where x ... select x); 

(查询为ObjectQuery< MyEntity>)。ToTraceString();

如果您想要一个简单的跟踪查看器(可视化外),将打印到跟踪日志... Studio)查看 DebugView



作为附加的附注,我使用/看到的最好的实时分析器是实体框架分析器,你必须支付它,但是有一个试用版可用,它将给你的SQL匹配的代码行。它也承认常见的问题。安装是愚蠢的简单...所有你必须添加的是一个声明在您的应用程序的初始化程序。






**编辑更新以显示如何使用用户代码**



我想我可以为您做所有的辛勤工作...;)。因为你使用的是一个懊恼的类型,只要把< T> 部分。

  var cklContactItems = from a in dbTestCenterViews.appvuChecklistExports 
其中a.MarketChecklistID == MCLID
&&& a.checklistSectionID == SID
&&& a.fieldGroupOrder!= null
orderby a.fieldGroupOrder升序
选择新的{
a.Column1,
a.Column2,
a.Column3,
a.Column4,
a.Column5,
a.Column1FieldID,
a.Column2FieldID,
a.Column3FieldID,
a.Column4FieldID,
a。 Column5FieldID,
a.fieldGroupOrderLabel
};

System.Diagnostics.Trace.WriteLine((查询为ObjectQuery).ToTraceString());


I an using VS2010, .NET4 and EF4. I would like to see the actual SQL that is generated when this is run. Also, what is this the best way to write this statement?

Here is my code:

var cklContactItems = from a in dbTestCenterViews.appvuChecklistExports
                                   where a.MarketChecklistID == MCLID 
                                        && a.checklistSectionID == SID 
                                        && a.fieldGroupOrder != null
                                   orderby a.fieldGroupOrder ascending
                                   select new { a.Column1, a.Column2, a.Column3, a.Column4, a.Column5,a.Column1FieldID,a.Column2FieldID,a.Column3FieldID,a.Column4FieldID,a.Column5FieldID,a.fieldGroupOrderLabel };

解决方案

var query = (from x in context.MyEntity where x... select x);

(query as ObjectQuery<MyEntity>).ToTraceString();

That will print to the trace log... if you want a simple trace viewer (outside of Visual Studio) check out DebugView

And as an additional side note, the best "real time" profiler I have used/seen out there is Entity Framework Profiler, you have to pay for it, but there is a trial version available, and it will give you SQL matched to the line of code. It also recognizes common "issues." Setup is stupid simple... all you have to add is one statement in the initializer of your application.


**Edit updated to show how to do it with users code **

I guess i can do all the hard work for you... ;). Since you use an annoynmous type just leave off the <T> part.

var cklContactItems = from a in dbTestCenterViews.appvuChecklistExports
                               where a.MarketChecklistID == MCLID 
                                    && a.checklistSectionID == SID 
                                    && a.fieldGroupOrder != null
                               orderby a.fieldGroupOrder ascending
                               select new {
                                   a.Column1, 
                                   a.Column2, 
                                   a.Column3, 
                                   a.Column4,
                                   a.Column5,
                                   a.Column1FieldID,
                                   a.Column2FieldID,
                                   a.Column3FieldID,
                                   a.Column4FieldID,
                                   a.Column5FieldID,
                                   a.fieldGroupOrderLabel 
                                  };

System.Diagnostics.Trace.WriteLine((query as ObjectQuery).ToTraceString());

这篇关于我如何找出这个语句产生的实际SQL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆