Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding the SQL output of a parameterized query

I'm making a parameterized query using C# against a SQL server 2005 instance, and I'd like to take a look at the SQL that is run against the database for debugging purposes. Is there somewhere I can look to see what the output SQL of the parameterized command is, either in the database logs or in the Visual Studio debugger?

like image 576
Dan Monego Avatar asked May 27 '10 17:05

Dan Monego


People also ask

What is a parameterized query in a SQL statement?

Parameterized SQL queries allow you to place parameters in an SQL query instead of a constant value. A parameter takes a value only when the query is executed, which allows the query to be reused with different values and for different purposes.

How do I write a parameterized query in SQL?

Declare statements start with the keyword DECLARE , followed by the name of the parameter (starting with a question mark) followed by the type of the parameter and an optional default value. The default value must be a literal value, either STRING , NUMERIC , BOOLEAN , DATE , or TIME .


2 Answers

Use SQL Server Profiler to view the sql

http://www.eggheadcafe.com/articles/sql_server_profiler.asp

http://msdn.microsoft.com/en-us/library/ms187929(SQL.105).aspx

like image 167
house9 Avatar answered Sep 28 '22 23:09

house9


SQL Profiler is the best solution, but if you need something more organic to your application that you could deploy and enable/disable in production, QA, etc... then you could build a wrapper around the System.Data.SqlClient Provider (Ex. the provider registered in the config file as... providerName="System.Data.SqlClient").

This would essentially act like an intercept proxy which would give you access to all the information passing through the Provider (e.g. between your application and the database client). This would allow you to siphon-off what you need, intercept, modify, aggregate and/or enrich it. This is a bit more advanced but opens the door to capture a whole range of information and could be inserted/replaced/removed as a separate layer of concern.

like image 31
JoeGeeky Avatar answered Sep 29 '22 00:09

JoeGeeky