How to obtain NHibernate generated SQL in code at runtime?

前端 未结 3 1427
-上瘾入骨i
-上瘾入骨i 2021-02-04 15:30

I know you can view the NHibernate generated SQL by hooking it up to log4net or piping it out to the console (\"show_sql\" option), but is there any way to obtain the generated

3条回答
  •  不思量自难忘°
    2021-02-04 15:45

    With NHibernate 3.2, this seems to work to get the SQL from an HQL query:

    private string GetSQL(string hql)
    {
        using (var iSession = ...)
        {
            var session = (NHibernate.Engine.ISessionImplementor)iSession;
            var sf = (NHibernate.Engine.ISessionFactoryImplementor)iSession.SessionFactory;
    
            var sql = new NHibernate.Engine.Query.HQLStringQueryPlan(hql, true, session.EnabledFilters, sf);
    
            return string.Join(";", sql.SqlStrings);
        }
    }
    

提交回复
热议问题