views:

1108

answers:

1

Basically I want to be able to do this:

session.ExecuteSql("...");

I don't need it to map to any entities or return any values. Any suggestions?

+2  A: 

As already mentioned, this is not a Fluent NHibernate issue but here is an example:

public int GetSqlCount<T>(Session session, string table)
{
    var sql = String.Format("SELECT Count(*) FROM {0}", table);
    var query = session.CreateSQLQuery(sql);
    var result = query.UniqueResult();
    // Could also use this if only updating values:
    //query.ExecuteUpdate();

    return Convert.ToInt32(result);
}

You will want to investigate the ISQLQuery interface, depending on your needs.

Steven Lyons
Is there a way to do this using a named query in fluent nhibernate instead of inline?
Kevin Berridge
There isn't a FNH-specific way but it should work fine with the NHiberbate configuration. As without FNH, put your named queries in an .hbm.xml file. Then configure FNH to load the mapping file along with your FNH mappings and you should be able to call the queries using the GetNamedQuery() method.
Steven Lyons