Adjusting CommandTimeout in Dapper.NET?

sh-beta picture sh-beta · Jan 9, 2012 · Viewed 50.9k times · Source

I'm trying to run SQL backups through a stored procedure through Dapper (the rest of my app uses Dapper so I'd prefer to keep this portion running through it as well). It works just fine until the CommandTimeout kicks in.

using (var c = SqlConnection(connstring))
{
    c.Open();
    var p = new DynamicParameters();
    // fill out p

    c.Execute("xp_backup_database", p, commandType: CommandType.StoredProcedure);
}

The only CommandTimeout setting I know of is in SqlCommand. Is there a way to set this via Dapper?

Answer

jzacharuk picture jzacharuk · Jan 9, 2012

Yes, there are multiple versions of the Execute function. One (or more) of them contains the commandTimeout parameters:

public static int Execute(this IDbConnection cnn, string sql, 
                dynamic param = null, IDbTransaction transaction = null, 
                            int? commandTimeout = null, CommandType? commandType = null)

Taken from SqlMapper.cs