Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get executed SQL from nHibernate

Tags:

c#

nhibernate

I am using nHibernate ICriteria to execute a query, and I would like to be able to get the SQL that was executed after the statement runs. So for example I have something like this.

ISession session = NHibernateSessionManager.Instance.GetSession();
DetachedCriteria query = BuildCriteria(); // Goes away and constructs the ICriteria
var result = query.GetExecutableCriteria(session).List<object>()

// somehow here get the sql that was just run
string sql = query.GetSqlSomehow();

I know I can log it and see the sql in the log, but I want to get it immediately after executing the statement so I can display the SQL to the user (even if it doesn't look nice).

like image 342
Craig Avatar asked Aug 12 '09 03:08

Craig


People also ask

How do I find NHibernate generated in SQL?

In the configuration settings, set the "show_sql" property to true. This will cause the SQL to be output in NHibernate's logfiles courtesy of log4net.

How do you call a stored procedure in NHibernate?

Calling the Stored ProcedureThere are a number of 'Set' methods (i.e. SetInt32) that allow you specify values for any parameters in the procedure. The AliasToBean method is then required to map the returned scalars (as specified in the XML) to the correct C# class.

What is NHibernate in C#?

NHibernate is an actively developed, fully featured, open source object-relational mapper for the . NET framework. It is used in thousands of successful projects. It's built on top of ADO.NET and the current version is NHibernate 4.0.


2 Answers

You can attach an IInterceptor to your NH ISession, then use the OnPrepareStatement() method to trap (even modify) the SQL.

like image 87
Vijay Patel Avatar answered Nov 07 '22 13:11

Vijay Patel


You can use Log4Net configuration to capture the SQL being used. To start you'd need to create a custom appender such as this:

using System;
using System.Collections.Generic;
using log4net.Appender;
using log4net.Core;

public class NHibernateQueryAppender : AppenderSkeleton
{
        private static List<string> s_queries = new List<string>();
    private static int s_queryCount = 0;

    public static IList<string> CurrentQueries
    {
           get { return s_queries.AsReadOnly(); }
    }

    public static int CurrentQueryCount
    {
        get { return s_queryCount; }
    }

    public static void Reset()
    {
        s_queryCount = 0;
        s_queries.Clear();
    }

    protected override void Append(LoggingEvent loggingEvent)
    {
        s_queries.Add(loggingEvent.RenderedMessage);
        s_queryCount++;
    }
}

Then configure log4net like so:

<log4net>
    <...other config...>

    <appender name="nhquerycheck" type="NHibernateExecutor.Loggers.NHibernateQueryAppender, NHibernateExecutor" />

    <logger name="NHibernate.SQL">
        <level value="DEBUG"/>
        <appender-ref ref="nhquerycheck" />
    </logger>
</log4net>

The above class can then be queried at runtime such as to display the sql output to screen


Edit: for some reason post didn't come out correctly, so found example on web http://nhforge.org/blogs/nhibernate/archive/2008/09/06/how-to-configure-log4net-for-use-with-nhibernate.aspx

like image 25
saret Avatar answered Nov 07 '22 13:11

saret