7

Lets take an example:

const string PERSON_SQL = "SELECT Id " +
                           "FROM Persons " +
                           "WHERE LastName=@LastName AND FirstName=@FirstName";
patientId = connection.ExecuteScalar<int>(PERSON_SQL, new
{
    LastName = _entity.Lastname,
    FirstName = _entity.Firstname
});

I would like to print out actual SQL query with parameter values for debugging purposes. I am sure there is some extension or helper function for it...

J Pollack
  • 2,788
  • 3
  • 29
  • 43

1 Answers1

2

Dapper doesn't include that functionality itself, the authors tend to use MiniProfiler for capturing SQL queries (see Marc Gravell's answer about something similar).

You could also use SQL Profiler, presuming you're using a SQL database.

Finally, if nothing "off the shelf" suits your needs, you could wrap the database connections and commands that you use with Dapper and capture / log the queries (and parameters) when ExecuteReader, ExecuteScalar, etc.. are called. I had some sample code for this in my answer to a question someone had about using Dapper with Access (though the sample code is database-agnostic, so you could use the "WrappedDbConnection" with whatever database you are using at the moment).

Community
  • 1
  • 1
Dan Roberts
  • 2,244
  • 16
  • 31
  • The SQL Profiler needs special permission TRACE to work. Is the same case with MiniProfiler? – J Pollack Sep 12 '16 at 07:27
  • The MiniProfiler examples are for a web app. Is there a C#/WPF MiniProfiler package? I guess using a wrapped DB connection would be the most suitable if there is not much code to rewrite. Otherwise a SQL profiler should be used. – J Pollack Sep 12 '16 at 07:39
  • There is a [MiniProfiler.Windows](https://github.com/nootn/MiniProfiler.Windows/) to answer to one of my own question, but it has not been updated for the last 4 years. (Although a pull request for update has been lately done.) – J Pollack Sep 12 '16 at 07:46