Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
90 views
in Technique[技术] by (71.8m points)

c# - Retrieve LINQ to sql statement (IQueryable) WITH parameters

I'm trying to figure out if there's a way to retrieve the (full) sql statement that gets executed on the database server.
I found something already, but it does not exactly what I would like:

IQueryable<SomeType> someQuery = ...
string command = dataContext.GetCommand(query).CommandText;

In my case this gives me a command string something like:

SELECT TOP (50) [t0].[ID], ....
FROM [dbo].[someTable] AS [t0]
WHERE ([t0].[someColumn] IS NOT NULL) AND (([t0].[someColumn]) IN (@p0))

On database there's executed:

exec sp_executesql N'SELECT TOP (50) [t0].[ID], ...
FROM [dbo].[someTable] AS [t0]
WHERE ([t0].[someColumn] IS NOT NULL) AND (([t0].[someColumn]) IN (@p0, @p1))',N'@p0  int,@p1 int',@p0=401,@p1=201

Is there a way to retrieve this 'full' statement (so also the parameter values) from C# code?

question from:https://stackoverflow.com/questions/6343754/retrieve-linq-to-sql-statement-iqueryable-with-parameters

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Once you get the Command you can print the CommandText and then loop through the Parameters collection and print all the individual parameters.

Also there is the linq-to-sql debug visualizer which does the same in debug mode.

A really nice tool to view the queries as they are happening is the Linq-to-sql profiler


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...