NHibernate在反编译项目中找到SQL select

时间:2012-11-26 15:58:06

标签: c# .net sql nhibernate

我有一个在IlSpy中反编译的项目和下面的代码。我在哪里可以找到原始SQL查询? criteria.List()如何知道哪个选择使用?

    protected object ListOne(ISession session, DbBaseArgs args)
    {
            ICriteria criteria = session.CreateCriteria(((DbListArgs)args).DbType);
            criteria.SetMaxResults(1);
            for (int i = 0; i < ((DbListArgs)args).DbExpressions.Count; i++)
            {
                if (((DbListArgs)args).DbExpressions[i] is Order)
                {
                    criteria.AddOrder((Order)((DbListArgs)args).DbExpressions[i]);

                }
                else
                {
                    criteria.Add((ICriterion)((DbListArgs)args).DbExpressions[i]);

                }
            IList results = criteria.List();
            object result;
            if (results != null && results.Count > 0)
            {
                result = results[0];
            }
            else
            {
                result = null;
            }
            return result;
}

2 个答案:

答案 0 :(得分:0)

反编译不会帮助您找到原始SQL查询。您可以尝试在调试级别为名为NHibernate.SQL的记录器添加日志,您应该在appender中看到发出的数据库查询。例如,在日志配置中添加如下内容:

<logger name="NHibernate.SQL" additivity="false">
    <level value="ALL"/>
    <appender-ref ref="NHibernateSQLFileLog"/>
</logger>

答案 1 :(得分:0)

您的选择是

  1. Run your app and view the log
  2. Write a bit of code to get the SQL to be generated
  3. 尝试NHProf
  4. (来自相关答案):

    public String GetGeneratedSql(ICriteria criteria)
    {
        var criteriaImpl = (CriteriaImpl) criteria;
        var sessionImpl = (SessionImpl) criteriaImpl.Session;
        var factory = (SessionFactoryImpl) sessionImpl.SessionFactory;
        var implementors = factory.GetImplementors(criteriaImpl.EntityOrClassName);
        var loader = new CriteriaLoader((IOuterJoinLoadable) factory.GetEntityPersister(implementors[0]), factory, criteriaImpl, implementors[0], sessionImpl.EnabledFilters);
    
        return loader.SqlString.ToString();
    }