cdub
cdub

Reputation: 25711

Getting actual SQL Query from paramaterized query in C# .NET debugger

I am trying to get the query so I can manipulate it and run it directly in SQL Server. It is a parametrized query like so in my .NET 4+ debugger in VS 2017:

SELECT DISTINCT name, 
CASE WHEN title = @searchTitle OR fullname = @searchFullName THEN 1 
ELSE ...
AS sorting
ORDER BY sorting ASC;

Ignore any syntax issues as the query is a sample and runs fine. I have the parameters get set correctly too, and as a note, there are tons of actual parameters so me copying and pasting is out of the question. The query is executed like so:

                return this.dataProvider.ExecuteMyLikeQueryTerms(
                     dr =>
                     {
                         var dt = new DataTable();
                         dt.Load(dr);

                         return dt;
                     },
                query,
                parameters,
                connection);

How can I get the SQL that is exectured with all the parameters added in so I can see it in the debugger and paste it into SQL Server? I have a very big query and am trying to trace it.

Upvotes: 2

Views: 339

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1062780

You already have the exact query being sent, which includes parameters (not parameter values). The closest you can get here is to fake some local variables:

declare @searchTitle nvarchar(max) = N'whatever';
-- etc

before your query (which is what MiniProfiler does, to make it possible to copy/paste runnable queries)

Upvotes: 1

Related Questions