如何找出该语句实际生成的 SQL?

发布于 2024-09-26 19:05:58 字数 690 浏览 0 评论 0原文

我使用 VS2010、.NET4 和 EF4。我想看看运行时生成的实际 SQL。另外,写这个声明的最佳方式是什么?

这是我的代码:

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 };

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 };

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

殤城〤 2024-10-03 19:05:58
var query = (from x in context.MyEntity where x... select x);

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

这将打印到跟踪日志...如果您想要一个简单的跟踪查看器(在 Visual Studio 之外),请查看 DebugView

另外,我用过/见过的最好的“实时”分析器是 Entity Framework Profiler,你必须付费,但有一个试用版可用,它会给你与代码行匹配的 SQL。它还认识到常见的“问题”。设置非常简单……您只需在应用程序的初始化程序中添加一条语句即可。


**编辑更新以显示如何使用用户代码执行此操作**

我想我可以为您完成所有艰苦的工作...;)。由于您使用了无名类型,因此只需省略 部分即可。

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());
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());
情话难免假 2024-10-03 19:05:58

您可以使用SQL Server ProfilerLinqPad,我确信还有其他工具

You can Use SQL Server Profiler. LinqPad, and Im sure there are other tools

祁梦 2024-10-03 19:05:58

您正在使用 VS10,并且有一个名为 Intellitrace 的工具。

You are using VS10 and there is a tool for this called Intellitrace.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文