@@ -10,16 +10,22 @@ namespace System.Data.SqlClient
1010{
1111 public static class DapperExtensions
1212 {
13- public static Task < int > ExecuteAsync ( this SqlConnection connection , string sql , SqlTransaction transaction = null , int timeout = 30 )
13+ public static Task < int > ExecuteAsync ( this SqlConnection connection , string sql , SqlTransaction transaction = null , TimeSpan ? commandTimeout = null )
1414 {
1515 SqlCommand cmd = connection . CreateCommand ( ) ;
1616 cmd . CommandText = sql ;
1717 cmd . CommandType = CommandType . Text ;
18- cmd . CommandTimeout = timeout ;
18+
19+ if ( commandTimeout . HasValue )
20+ {
21+ cmd . CommandTimeout = ( int ) commandTimeout . Value . TotalSeconds ;
22+ }
23+
1924 if ( transaction != null )
2025 {
2126 cmd . Transaction = transaction ;
2227 }
28+
2329 return cmd . ExecuteNonQueryAsync ( ) ;
2430 }
2531
@@ -28,7 +34,7 @@ public static async Task<IEnumerable<T>> QueryWithRetryAsync<T>(
2834 string sql ,
2935 object param = null ,
3036 IDbTransaction transaction = null ,
31- int ? commandTimeout = null ,
37+ TimeSpan ? commandTimeout = null ,
3238 CommandType ? commandType = null ,
3339 int maxRetries = 10 ,
3440 Action onRetry = null )
@@ -37,7 +43,7 @@ public static async Task<IEnumerable<T>> QueryWithRetryAsync<T>(
3743 {
3844 try
3945 {
40- return await connection . QueryAsync < T > ( sql , param , transaction , commandTimeout , commandType ) ;
46+ return await connection . QueryAsync < T > ( sql , param , transaction , ( int ? ) commandTimeout ? . TotalSeconds , commandType ) ;
4147 }
4248 catch ( SqlException ex )
4349 {
@@ -68,15 +74,16 @@ public static async Task<IEnumerable<T>> QueryWithRetryAsync<T>(
6874 }
6975 }
7076 }
77+
7178 throw new Exception ( "Unknown error! Should have thrown the final timeout!" ) ;
7279 }
73-
80+
7481 public static async Task < T > ExecuteScalarWithRetryAsync < T > (
7582 this SqlConnection connection ,
7683 string sql ,
7784 object param = null ,
7885 IDbTransaction transaction = null ,
79- int ? commandTimeout = null ,
86+ TimeSpan ? commandTimeout = null ,
8087 CommandType ? commandType = null ,
8188 int maxRetries = 10 ,
8289 Action onRetry = null )
@@ -85,7 +92,7 @@ public static async Task<T> ExecuteScalarWithRetryAsync<T>(
8592 {
8693 try
8794 {
88- return await connection . ExecuteScalarAsync < T > ( sql , param , transaction , commandTimeout , commandType ) ;
95+ return await connection . ExecuteScalarAsync < T > ( sql , param , transaction , ( int ? ) commandTimeout ? . TotalSeconds , commandType ) ;
8996 }
9097 catch ( SqlException ex )
9198 {
@@ -116,7 +123,8 @@ public static async Task<T> ExecuteScalarWithRetryAsync<T>(
116123 }
117124 }
118125 }
126+
119127 throw new Exception ( "Unknown error! Should have thrown the final timeout!" ) ;
120128 }
121129 }
122- }
130+ }
0 commit comments