سؤال

This is probably fairly straightforward but i can't seem to find a reasonable explanation in any documentation.

I'm trying to use an NHibernate.ISQLQuery and using SetResultTransformer() to return a custom set of results from a custom SQL query. Like so:

    public virtual IList<T> GetSQLObject<T>(string sql, IDbParameter[] parameters = null)
    {
        ISQLQuery qry = _sess.CreateSQLQuery(sql);

        qry.SetResultTransformer(Transformers.AliasToBean(typeof(T)));    

        if (parameters != null) {
            foreach (IDbParameter parameter in parameters) {
                qry.SetParameter(parameter.Name, parameter.Value);
            }
        }

        return qry.List<T>();
    }

From looking at the examples, it seems that in the sql query I have to use parameters in the format :param1 instead of @param1 as I would in a standard SQL query. If i use the latter syntax in the query, it throws an error at qry.SetParameter().

Is there a reason why ISQLQuery/NHibernate requires them in this format and won't work with the normal syntax?

هل كانت مفيدة؟

المحلول

SQL Server uses @param, but not every other database does. For example, MySQL uses ?param

NHibernate allows you to swap out 1 database implementation for another with little to no reworking of your DAL. It sets the parameters based on the database you configured when you setup the NH Configuration.

Edit: Also I think :param came about from Hibernate being targeted at Oracle when it was initially developed, since Oracle uses :param

نصائح أخرى

Phil has answered the "why"; so perhaps I can recommend a "how"; why not just add a new extension method to the IDbParameter type (something like .GetNHibernateName() ) that will return the parameter name with the "@" replaced with a ":"; that should be trivial to implement.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top