惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

B
Blog RSS Feed
C
CERT Recently Published Vulnerability Notes
P
Proofpoint News Feed
Y
Y Combinator Blog
T
The Blog of Author Tim Ferriss
云风的 BLOG
云风的 BLOG
H
Help Net Security
Recorded Future
Recorded Future
The Register - Security
The Register - Security
F
Full Disclosure
N
Netflix TechBlog - Medium
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hackread – Cybersecurity News, Data Breaches, AI and More
爱范儿
爱范儿
Security Archives - TechRepublic
Security Archives - TechRepublic
Simon Willison's Weblog
Simon Willison's Weblog
Cisco Talos Blog
Cisco Talos Blog
I
InfoQ
T
Tenable Blog
T
Tor Project blog
人人都是产品经理
人人都是产品经理
D
DataBreaches.Net
NISL@THU
NISL@THU
Google DeepMind News
Google DeepMind News
博客园 - 叶小钗
B
Blog
V
V2EX
Jina AI
Jina AI
L
LangChain Blog
月光博客
月光博客
W
WeLiveSecurity
U
Unit 42
AWS News Blog
AWS News Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
博客园 - 聂微东
V
Visual Studio Blog
A
Arctic Wolf
T
Tailwind CSS Blog
The Cloudflare Blog
SecWiki News
SecWiki News
S
SegmentFault 最新的问题
Hacker News - Newest:
Hacker News - Newest: "LLM"
宝玉的分享
宝玉的分享
MyScale Blog
MyScale Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
S
Securelist
www.infosecurity-magazine.com
www.infosecurity-magazine.com
腾讯CDC
雷峰网
雷峰网

博客园 - amingo

面向对象设计(Object-Oriented Design,OOD) 文科妹学 GitHub 简易教程 用户中心 - 博客园 SSIS 2010 BUG 一例 甲骨文公司面试题 数据仓库(二)Architecture 数据仓库(一)Introduction 【免费】承接各类SharePoint项目开发工作 【免费】承接翻译各类英文技术文章 SharePoint Workflow 版本控制 SharePoint Workflow Debugging 我断言今年世界杯将会是一支新队夺冠! JavaScript语言精髓(1)之语法概要拾遗 JavaScript语言精髓(0) Linq在路上(一)一统江湖 Linq在路上(序) 关于管理的那些事 两分钟明白什么是ERP 白话CMMI
Linq在路上(外一)获取SQL语句
amingo · 2009-09-08 · via 博客园 - amingo

  Linq方便了我们对数据库的操作,直接用C#的语法操作数据库。但Linq也隐藏了实际执行的SQL语句,封装是好事,可是有时候还是不得不了解Linq具体对数据库的操作。比如,只有查看实际的SQL语句才能对数据库或查询语句做优化,而且在调试时不能查看SQL是很郁闷的事情。

有下面几种方法来挖掘出操作中所使用的Sql语句:

1、获取Query所对应的SqlCommand对象:

  在开发过程中,我们可以通过Query获得对应的Sql Command对象。请看如下代码:

AdventureWorksDataContext db = new AdventureWorksDataContext();
var products = from p in db.Products
               where p.ProductID == 3
               select new { p.ProductID, p.Name };
foreach (var p in products)
{
    Console.WriteLine(p);
}
 
DbCommand cmd = db.GetCommand(products);
 
Console.WriteLine("------------");
Console.WriteLine("Command Text: \n{0}", cmd.CommandText);
 
Console.WriteLine("------------");
Console.WriteLine("Command Type: \n{0}", cmd.CommandType);
 
Console.WriteLine("------------");
Console.WriteLine("Command Parameters:");
foreach (DbParameter p in cmd.Parameters)
{
    Console.WriteLine("{0}: {1}", p.ParameterName, p.Value);
}
 
Console.ReadLine();

Command Text:
SELECT [t0].[ProductID], [t0].[Name]
FROM [Production].[Product] AS [t0]
WHERE [t0].[ProductID] = @p0
------------
Command Type:
Text
------------
Command Parameters:
@p0: 3

  

可以看到,无论是Sql语句或是参数都被打印了出来。事实上,由于我们得到了完整的SqlCommand对象,我们可以获取的信息并不止上述这些。

2、使用LINQ to SQL Debug Visualizer:

  使用LINQ to SQL Debug Visiualizer,我们可以在调试程序时直观地获得Query所对应的Sql语句以及参数,而不必获得SqlCommand对象并打印信息。具体使用方法详见Scott Gu的这篇博文

3、使用DataContext的Log功能:

  DataContext自带的Log属性为一个TextWriter类型的对象,如果我们设置了这个属性,则DataContext所有的 操作将会通过这个TextWriter对象输出。与比上述两种方法相比,这个方法的优势在于DataContext所执行的所有语句,无论SELECT、 INSERT、UPDATE或者是DELETE都会被输出;而上面的两种做法只能得到Query的信息,也就是SQL语句的SELECT操作。

  请看如下代码,下面的代码将AdventureWorksDataContext对象的所有操作输出至Console:

AdventureWorksDataContext db = new AdventureWorksDataContext();
db.Log = Console.Out;
 
Product product = (from p in db.Products
                   where p.ProductID == 1
                   select p).First();
 
product.Name = "Hello World";
db.SubmitChanges();
 
Console.ReadLine();

SELECT TOP (1) [t0].[ProductID], [t0].[Name], [t0].[ProductNumber], [t0].[MakeFl
...
edDate], [t0].[rowguid], [t0].[ModifiedDate]
FROM [Production].[Product] AS [t0]
WHERE [t0].[ProductID] = @p0
-- @p0: Input Int (Size = 0; Prec = 0; Scale = 0) [1]
-- Context: SqlProvider(Sql2005) Model: AttributedMetaModel Build: 3.5.21004.1

UPDATE [Production].[Product]
SET [Name] = @p11
WHERE ([ProductID] = @p0) AND ([Name] = @p1) AND ([ProductNumber] = @p2) AND (NO
...
NULL) AND ([rowguid] = @p9) AND ([ModifiedDate] = @p10)
-- @p0: Input Int (Size = 0; Prec = 0; Scale = 0) [1]
...
-- @p11: Input NVarChar (Size = 11; Prec = 0; Scale = 0) [Hello World]
-- Context: SqlProvider(Sql2005) Model: AttributedMetaModel Build: 3.5.21004.1

  

在这里我省略了大部分的输出,不过从上面的片断中我们已经可以看到SELECT和UPDATE操作所使用的Sql语句以及参数都被打印了出来。这就是我们可以利用的调试信息。

4、使用Sql Server Profiler:

  这个是我推荐的方法,比1,3方便,而且不可能漏掉任何信息。Sql Profiler的用法就不说了,大家应该都用过,但和监视一般的SQL还是有点地方需要注意。

aaaDataContext db = new aaaDataContext();
aaa a = new aaa();
var q =
            from p in context.aaa
            where p.a.StartsWith("a") == true
            select p.id;

int? a1 = q.First();

上面这段代码Linq不是直接将它转换成"select id from aaa where....."而是转换成

  

exec sp_executesql N'SELECT TOP (1) [t0].[id]
FROM [dbo].[aaa] AS [t0]
WHERE [t0].[a] LIKE @p0',N'@p0 nvarchar(2)',@p0=N'a%'


  使用了存储过程sp_executesql以及动态代码的方式,因此,想要获取上述这段SQL需要打开profiler事件选择中stored procedures下的RPC:Completed和SP:Completed事件。而且由于Linq使用延迟加载机制来减少性能消耗,因此只有在int? a1 = q.First();执行时,sql语句才真正被执行。

鼓励作者写出更好的文章

每个人都会经过这个阶段,见到一座山,就想知道山后面是什么。我很想告诉他,可能翻过山后面,你会发现没什么特别。回望之下,可能会觉得这一边更好。 每个人都会坚持自己的信念,在别人看来,是浪费时间,她却觉得很重要。