本文介绍了从 nHibernate 获取执行的 SQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 nHibernate ICriteria 来执行查询,我希望能够获取在语句运行后执行的 SQL.所以例如我有这样的东西.

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();

我知道我可以记录它并查看日志中的 sql,但我想在执行语句后立即获取它,以便我可以向用户显示 SQL(即使它看起来不好看).

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).

推荐答案

您可以将 IInterceptor 附加到您的 NH ISession,然后使用 OnPrepareStatement() 捕获(甚至修改) SQL.

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

这篇关于从 nHibernate 获取执行的 SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 03:45