Micah
Micah

Reputation: 116090

Executing Sql statements with Fluent NHibernate

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?

Upvotes: 20

Views: 30413

Answers (1)

Steven Lyons
Steven Lyons

Reputation: 8218

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.

Upvotes: 37

Related Questions