How to set the CommandTimeout value in Dapper?

.net c# dapper timeout

Question

I'm attempting to utilize a stored procedure in Dapper to execute SQL backups (because the rest of my project already uses Dapper, I'd want to keep it that way). Up to the CommandTimeout, it operates without a hitch.

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

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

SqlCommand contains the only CommandTimeout setting I'm aware of. Is it possible to configure this using Dapper?

1
77
1/9/2012 8:36:58 PM

Accepted Answer

The Execute function does indeed come in different iterations. The commandTimeout parameters are present in one (or more) of them:

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

from zSqlMapper.csz

92
10/25/2017 3:08:30 PM

Popular Answer

If anybody is interested, here is an example from the original question with the extra correct answer. (Timeout duration is 60 seconds):

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

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


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow