11

I will like to know if is possible to see what the SQL statement actully looks after the parameters have been parsed in to the SQL string. For example given the following code:

const string SQL = @"
SELECT *
   FROM V_PROVIDER_WAGE_OVER_REVENUE
WHERE SITE_ID = :SITE_ID 
   AND VENDOR_ID = :VENDOR_ID
   AND date_paid >= :DATE_FROM 
   AND date_paid <= :DATE_TO;";

conn.Query<ProviderWageOverRevenueModel>(SQL, new { 
     VENDOR_ID = vendorId, 
     SITE_ID = siteId, 
     DATE_FROM = dateFrom, 
     DATE_TO = dateTo 
}).ToList();

I will like to know if at some point (maybe inside Dapper source?) can I see the something like:

SELECT *
   FROM V_PROVIDER_WAGE_OVER_REVENUE
WHERE SITE_ID = 199
   AND VENDOR_ID = 17
   AND date_paid >= '01/01/2014' 
   AND date_paid <= '20/11/2014';

Edit

I was assuming that the parameters placeholders will be replaced with the values, however according after looking closely at the source of Dapper I realize that its just relaying on a iDbCommand to executed the query, so after looking at this question It is possible to see executed statement inside .Net?

I try this:

string query = cmd.CommandText;
foreach (DbParameter p in cmd.Parameters)
{
    query = query.Replace(p.ParameterName, p.Value.ToString());
}

however the result is not seem to be like I hope:

SELECT *
    FROM V_PROVIDER_WAGE_OVER_REVENUE
WHERE 23411127316 = :23411127316 
    AND 23411076816 = :23411076816
    AND date_paid >= :1/07/2014 10:11:34 PM 
    AND date_paid <= :12/12/2014 12:00:00 AM;
Community
  • 1
  • 1
dnlgmzddr
  • 628
  • 1
  • 7
  • 25
  • Are you assuming that Dapper replace the parameter placeholders with the actual value of your parameters? – Steve Dec 12 '14 at 22:12
  • Yes I was assuming that the parameters placeholders will be replaced with values, however according to [link](http://stackoverflow.com/questions/2789476/is-it-possible-to-get-the-parsed-text-of-a-sqlcommand-with-sqlparameters) and it looking at the Source dapper just relay on a IDbCommand. – dnlgmzddr Dec 12 '14 at 22:18
  • 1
    Exactly. Not only Dapper but every other NET provider works in this way. You could see the real sql received by the server using some kind of monitoring tool on the server. From the parameter prefix this could be MySql or Oracle, you need to search in their support tools (Sql Server has the Profiler) – Steve Dec 12 '14 at 22:27
  • Thank you @Steve I was just editing the question while you reply. In fact it's Oracle I will take a look at the support tools. Have a great day and again thank you. – dnlgmzddr Dec 12 '14 at 22:33

1 Answers1

5

In terms of the fundamental question: the easiest way to do this is with a tool like mini-profiler and a wrapped connection.

Dapper does have some query manipulation things - but actually: not in the case shown. So the simple answer is that the SQL executed is exactly the contents of SQL.

Most ADO.NET providers support parameters all the way down to the database. If your provider does not, then it won't be visible except via your database's own monitoring tools.

cheesemacfly
  • 11,622
  • 11
  • 53
  • 72
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900