ExecuteNonQuery() alternative that calls ExecFilter

I need to execute a non-query but have it process through the ExecFilter.

The BCL method ExecuteNonQuery obviously doesn’t use the filter.

OrmLiteConfig.ExecFilter = new SpecialExecFilter();

using (var cmd = db.SqlProc("SomeSp", new {
    A = a,
    B = b
    var outputParam = cmd.AddParam("OutputParam", null, ParameterDirection.Output);
    cmd.ExecuteNonQuery(); // Doesn't call ExecFilter :( 
    return outputParam.Value;

I was looking for an SS extension method like ExecNonQuery, but can’t find anything appropriate.

(I’m using SS 4.0.56)

OrmLite provides ExecNonQuery() which calls ResultsFilter.ExecuteSql().

Hi Mythz

That method is internal, but even if it was public I can’t see that it would call the ExecFilter.

Generally, should the ExecFilter be called for all OrmLite methods? Maybe I’m misunderstanding it’s purpose. I need something to happen before and after every query (select, insert, exec sp, whatever).

It’s just a normal OrmLite method, the public ExecNonQuery API is here.

What about when you already have the dbCmd though, I’m using SqlProc which returns the dbCmd, how can I execute it and have the ExecFilter called?

Are you suggesting not using SqlProc and using raw SQL?

It’s not available at the IDbCommand level, the managed Exec filter is what creates the creates the command that’s used, so your top-level SqlProc would use it, but not your cmd callback.

SqlProc ends up using the non-virtual overload so the custom behaviour in my filter isn’t executed. I think this makes sense as the command isn’t actually executed by OrmLite.

Can OrmLite be extended to allow the SqlProc prepared command be executed via the ExecFilter?

I’ve made all methods on OrmLiteExecFilter virtual in this commit which is available from v4.0.61 on MyGet.

If that still doesn’t do what you want can you send a PR that does, as I’m having trouble following as SqlProc already calls db.Exec() which is executed on the Exec filter (like all other APIs) and the only API’s on IOrmLiteExecFilter expose is a db connection not a command so you may instead need to create your own extension method, e.g. IDbCommand.NonQueryExec() and have it apply the custom logic you want before you call ADO.NET’s ExecuteNonQuery().

Thanks. I think a custom extension method is the way to go then.