133

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?

1
  • 2
    For whatever reason, I can't answer my own question right now. But it seems just adding the named argument "commandTimeout: 0" to c.Execute() took care of this.
    – sh-beta
    Jan 9, 2012 at 21:15

4 Answers 4

142

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

8
  • 6
    I had the same problem, but with Query method, however the solution worked for it too, as it has a commandTimeout parameter too.
    – jahu
    May 12, 2015 at 9:11
  • 2
    @DrSchizo why would it not be used, there's no reason for Async Await to avoid the Time out Mar 31, 2017 at 6:49
  • 1
    @DrSchizo The docs say its not used with asynchronous methods like BeginExecuteReader, not async/await. I assume this is because if you use BeginExecuteReader, it is assumed you will use your own timeout logic.
    – jugg1es
    Dec 2, 2017 at 21:43
  • 3
    Is it possible to set this timeout for all queries? I tried with the SqlConnection.ConnectionTimeout Property but it says that it's read only. I would need it for some custom migration programs. It's tedious to type it with every statement.
    – ttt
    Jan 29, 2018 at 14:08
  • 7
    @jedatkinports SqlMapper.Settings.CommandTimeout I believe is what you are after.
    – Shiv
    Jul 24, 2018 at 20:26
77

Example from original question with accepted answer added, in case anyone wants it. (Timeout is set to 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);
}
0
52

There is no need to set command timeout for all queries/Db Calls. You can set it globally like below.

Dapper.SqlMapper.Settings.CommandTimeout = 0;

You can initialize this static property on the application load or in the database class constructor.

This helps in removing duplication, and in case you decide to change it later, you change it once in one place.

2
  • where do we put this command? Oct 18, 2022 at 3:06
  • @JohnathanLi It depends on the type of your application. but usually somewhere in the initialization code of you app.
    – Mozart
    Oct 18, 2022 at 9:41
6

I was able to solve my problem using connection.Query setting the timeout directly

int timeOutInSeconds = 60;
.
.
.
result = conn.Query<list>(stringQuery, new {parameters, ..}, null, true, timeOutInSeconds).ToList();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.