Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug parameterized SQL query

Tags:

c#

sql

I use C# to make connection to a db and then a Ad hoc SQL to get data. This simple SQL query is very convenient to debug since I can log the SQL query string. If I use parametrized SQL query command, is there any way to log sql query string for debug purpose?

like image 963
David.Chu.ca Avatar asked Dec 24 '08 06:12

David.Chu.ca


People also ask

How do you debug a SQL query?

To debug a function, open the procedure calling that function and insert a breakpoint for the function you want to debug. Then, start debugging. Step through the code using the F11 key or Step Into, or press CTRL+F5 to move directly to the breakpoint. Press F11 or click Step Into to get inside the stored function.

How does SQL parameterized query work?

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.


1 Answers

I think this is about it. Place this code where you have configured the query command and you'll have the into debugSQL the SQL statement which will be executed

string debugSQL = cmd.CommandText;

foreach (SqlParameter param in cmd.Parameters)
{
    debugSQL = debugSQL.Replace(param.ParameterName, param.Value.ToString());
}
like image 132
ɐsɹǝʌ ǝɔıʌ Avatar answered Sep 28 '22 05:09

ɐsɹǝʌ ǝɔıʌ