Setting CommandTimeout in Dapper.NET?

I'm trying to start SQL backup through a stored procedure through Dapper (the rest of my application uses Dapper, so I would prefer this part to work through it as well). It works fine until CommandTimeout appears.

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 parameter that I know of is in SqlCommand. Is there any way to install this through Dapper?

+55
c # timeout dapper
Jan 09 '12 at 20:36
source share
2 answers

Yes, there are several versions of the Execute function. One (or more) of them contains 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

+59
Jan 09 2018-12-21T00:
source share

An example from the original question with the accepted answer is added if someone wants it. (Timeout 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); } 
+22
Aug 13 '15 at 18:38
source share



All Articles