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?
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.
It seems that people are confused as to whether this is seconds or milliseconds. The documentation states that the timeout is in seconds. A 1-minute timeout seems reasonable for most queries.
The time in seconds to wait for the command to execute.
The CommandTimeout property sets or returns the number of seconds to wait while attempting to execute a command, before canceling the attempt and generate an error. Default is 30.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With