Adjusting CommandTimeout in Dapper.NET?
Solution 1:
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
Solution 2:
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);
}
Solution 3:
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.
Solution 4:
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();