@@ -136,12 +136,13 @@ public static DbCommand AddParameter(this DbCommand command, string id, DbType t
136
136
/// Begins a transaction
137
137
/// </summary>
138
138
/// <param name="command">Command object</param>
139
+ /// <param name="retries">The retries.</param>
139
140
/// <returns>A transaction object</returns>
140
- public static DbTransaction BeginTransaction ( this DbCommand command )
141
+ public static DbTransaction BeginTransaction ( this DbCommand command , int retries = 0 )
141
142
{
142
143
if ( command == null || command . Connection == null )
143
144
return null ;
144
- command . Open ( ) ;
145
+ command . Open ( retries ) ;
145
146
command . Transaction = command . Connection . BeginTransaction ( ) ;
146
147
return command . Transaction ;
147
148
}
@@ -187,28 +188,32 @@ public static DbCommand Commit(this DbCommand command)
187
188
/// <summary>
188
189
/// Executes the stored procedure as a scalar query
189
190
/// </summary>
191
+ /// <typeparam name="DataType">The type of the ata type.</typeparam>
190
192
/// <param name="command">Command object</param>
191
193
/// <param name="defaultValue">Default value if there is an issue</param>
194
+ /// <param name="retries">The retries.</param>
192
195
/// <returns>The object of the first row and first column</returns>
193
- public static DataType ExecuteScalar < DataType > ( this DbCommand command , DataType defaultValue = default ( DataType ) )
196
+ public static DataType ExecuteScalar < DataType > ( this DbCommand command , DataType defaultValue = default ( DataType ) , int retries = 0 )
194
197
{
195
198
if ( command == null )
196
199
return defaultValue ;
197
- command . Open ( ) ;
200
+ command . Open ( retries ) ;
198
201
return command . ExecuteScalar ( ) . To ( defaultValue ) ;
199
202
}
200
203
201
204
/// <summary>
202
205
/// Executes the stored procedure as a scalar query async
203
206
/// </summary>
207
+ /// <typeparam name="DataType">The type of the ata type.</typeparam>
204
208
/// <param name="command">Command object</param>
205
209
/// <param name="defaultValue">Default value if there is an issue</param>
210
+ /// <param name="retries">The retries.</param>
206
211
/// <returns>The object of the first row and first column</returns>
207
- public static async Task < DataType > ExecuteScalarAsync < DataType > ( this DbCommand command , DataType defaultValue = default ( DataType ) )
212
+ public static async Task < DataType > ExecuteScalarAsync < DataType > ( this DbCommand command , DataType defaultValue = default ( DataType ) , int retries = 0 )
208
213
{
209
214
if ( command == null )
210
215
return defaultValue ;
211
- command . Open ( ) ;
216
+ command . Open ( retries ) ;
212
217
var ReturnValue = await command . ExecuteScalarAsync ( ) ;
213
218
return ReturnValue . To ( defaultValue ) ;
214
219
}
@@ -254,14 +259,28 @@ public static DbParameter GetOrCreateParameter(this DbCommand command, string id
254
259
/// Opens the connection
255
260
/// </summary>
256
261
/// <param name="command">Command object</param>
262
+ /// <param name="retries">The retries.</param>
257
263
/// <returns>The DBCommand object</returns>
258
- public static DbCommand Open ( this DbCommand command )
264
+ public static DbCommand Open ( this DbCommand command , int retries = 0 )
259
265
{
260
- if ( command != null
261
- && command . Connection != null
262
- && command . Connection . State != ConnectionState . Open )
263
- command . Connection . Open ( ) ;
264
- return command ;
266
+ Exception holder = null ;
267
+ while ( retries >= 0 )
268
+ {
269
+ try
270
+ {
271
+ if ( command != null
272
+ && command . Connection != null
273
+ && command . Connection . State != ConnectionState . Open )
274
+ command . Connection . Open ( ) ;
275
+ return command ;
276
+ }
277
+ catch ( Exception e )
278
+ {
279
+ holder = e ;
280
+ }
281
+ -- retries ;
282
+ }
283
+ throw holder ;
265
284
}
266
285
267
286
/// <summary>
0 commit comments