@@ -20,6 +20,7 @@ limitations under the License.
2020using System ;
2121using System . Data ;
2222using System . Data . Common ;
23+ using System . Diagnostics . CodeAnalysis ;
2324using System . Linq ;
2425using System . Threading . Tasks ;
2526
@@ -33,13 +34,13 @@ public static class DbCommandExtensions
3334 /// <summary>
3435 /// The bad database types
3536 /// </summary>
36- private static readonly DbType [ ] BadDbTypes = {
37+ private static readonly DbType [ ] _BadDbTypes = [
3738 DbType . Time ,
3839 DbType . SByte ,
3940 DbType . UInt16 ,
4041 DbType . UInt32 ,
4142 DbType . UInt64
42- } ;
43+ ] ;
4344
4445 /// <summary>
4546 /// Adds a parameter to the call (for strings only)
@@ -55,10 +56,8 @@ public static DbCommand AddParameter(
5556 string value = "" ,
5657 ParameterDirection direction = ParameterDirection . Input )
5758 {
58- if ( command is null )
59- throw new ArgumentNullException ( nameof ( command ) ) ;
60- if ( string . IsNullOrEmpty ( id ) )
61- throw new ArgumentNullException ( nameof ( id ) ) ;
59+ ArgumentNullException . ThrowIfNull ( command , nameof ( command ) ) ;
60+ ArgumentNullException . ThrowIfNullOrEmpty ( id , nameof ( id ) ) ;
6261 var Length = string . IsNullOrEmpty ( value ) ? 1 : value . Length ;
6362 if ( direction == ParameterDirection . Output
6463 || direction == ParameterDirection . InputOutput
@@ -68,8 +67,8 @@ public static DbCommand AddParameter(
6867 Length = - 1 ;
6968 }
7069
71- var Parameter = command . GetOrCreateParameter ( id ) ;
72- Parameter . Value = string . IsNullOrEmpty ( value ) ? DBNull . Value : ( object ) value ;
70+ DbParameter Parameter = command . GetOrCreateParameter ( id ) ;
71+ Parameter . Value = string . IsNullOrEmpty ( value ) ? DBNull . Value : value ;
7372 Parameter . IsNullable = string . IsNullOrEmpty ( value ) ;
7473 Parameter . DbType = DbType . String ;
7574 Parameter . Direction = direction ;
@@ -94,10 +93,8 @@ public static DbCommand AddParameter(
9493 object ? value = null ,
9594 ParameterDirection direction = ParameterDirection . Input )
9695 {
97- if ( command is null )
98- throw new ArgumentNullException ( nameof ( command ) ) ;
99- if ( string . IsNullOrEmpty ( id ) )
100- throw new ArgumentNullException ( nameof ( id ) ) ;
96+ ArgumentNullException . ThrowIfNull ( command , nameof ( command ) ) ;
97+ ArgumentNullException . ThrowIfNullOrEmpty ( id , nameof ( id ) ) ;
10198 return command . AddParameter ( id , type . To < DbType > ( ) , value , direction ) ;
10299 }
103100
@@ -114,16 +111,14 @@ public static DbCommand AddParameter(
114111 public static DbCommand AddParameter < TDataType > (
115112 this DbCommand command ,
116113 string id ,
117- TDataType value = default ,
114+ TDataType ? value = default ,
118115 ParameterDirection direction = ParameterDirection . Input )
119116 {
120- if ( command is null )
121- throw new ArgumentNullException ( nameof ( command ) ) ;
122- if ( string . IsNullOrEmpty ( id ) )
123- throw new ArgumentNullException ( nameof ( id ) ) ;
117+ ArgumentNullException . ThrowIfNull ( command , nameof ( command ) ) ;
118+ ArgumentNullException . ThrowIfNullOrEmpty ( id , nameof ( id ) ) ;
124119 return command . AddParameter (
125120 id ,
126- GenericEqualityComparer < TDataType > . Comparer . Equals ( value , default ! ) ? typeof ( TDataType ) . To < DbType > ( ) : value ? . GetType ( ) . To < DbType > ( ) ?? DbType . Int32 ,
121+ GenericEqualityComparer < TDataType > . Comparer . Equals ( value ! , default ! ) ? typeof ( TDataType ) . To < DbType > ( ) : value ? . GetType ( ) . To < DbType > ( ) ?? DbType . Int32 ,
127122 value ,
128123 direction ) ;
129124 }
@@ -145,11 +140,9 @@ public static DbCommand AddParameter(
145140 object ? value = null ,
146141 ParameterDirection direction = ParameterDirection . Input )
147142 {
148- if ( command is null )
149- throw new ArgumentNullException ( nameof ( command ) ) ;
150- if ( string . IsNullOrEmpty ( id ) )
151- throw new ArgumentNullException ( nameof ( id ) ) ;
152- var Parameter = command . GetOrCreateParameter ( id ) ;
143+ ArgumentNullException . ThrowIfNull ( command , nameof ( command ) ) ;
144+ ArgumentNullException . ThrowIfNullOrEmpty ( id , nameof ( id ) ) ;
145+ DbParameter Parameter = command . GetOrCreateParameter ( id ) ;
153146 Parameter . IsNullable = value is null || DBNull . Value == value ;
154147 if ( Parameter . IsNullable )
155148 {
@@ -163,7 +156,7 @@ public static DbCommand AddParameter(
163156 {
164157 Parameter . Value = value ;
165158 }
166- if ( type != default && ! BadDbTypes . Contains ( type ) )
159+ if ( type != default && ! _BadDbTypes . Contains ( type ) )
167160 Parameter . DbType = type ;
168161 Parameter . Direction = direction ;
169162 return command ;
@@ -179,7 +172,7 @@ public static DbCommand AddParameter(
179172 {
180173 if ( command ? . Connection is null )
181174 return null ;
182- command . Open ( retries ) ;
175+ _ = command . Open ( retries ) ;
183176 command . Transaction = command . Connection . BeginTransaction ( ) ;
184177 return command . Transaction ;
185178 }
@@ -202,7 +195,7 @@ public static DbCommand AddParameter(
202195 /// <returns>The DBCommand object</returns>
203196 public static DbCommand ? Close ( this DbCommand command )
204197 {
205- if ( ! ( command ? . Connection is null )
198+ if ( command ? . Connection is not null
206199 && command . Connection . State != ConnectionState . Closed )
207200 {
208201 command . Connection . Close ( ) ;
@@ -230,11 +223,12 @@ public static DbCommand AddParameter(
230223 /// <param name="defaultValue">Default value if there is an issue</param>
231224 /// <param name="retries">The retries.</param>
232225 /// <returns>The object of the first row and first column</returns>
233- public static TDataType ExecuteScalar < TDataType > ( this DbCommand command , TDataType defaultValue = default , int retries = 0 )
226+ [ return : NotNullIfNotNull ( nameof ( defaultValue ) ) ]
227+ public static TDataType ? ExecuteScalar < TDataType > ( this DbCommand command , TDataType ? defaultValue = default , int retries = 0 )
234228 {
235229 if ( command is null )
236230 return defaultValue ;
237- command . Open ( retries ) ;
231+ _ = command . Open ( retries ) ;
238232 return command . ExecuteScalar ( ) . To ( defaultValue ) ;
239233 }
240234
@@ -246,11 +240,12 @@ public static TDataType ExecuteScalar<TDataType>(this DbCommand command, TDataTy
246240 /// <param name="defaultValue">Default value if there is an issue</param>
247241 /// <param name="retries">The retries.</param>
248242 /// <returns>The object of the first row and first column</returns>
249- public static async Task < TDataType > ExecuteScalarAsync < TDataType > ( this DbCommand command , TDataType defaultValue = default , int retries = 0 )
243+ [ return : NotNullIfNotNull ( nameof ( defaultValue ) ) ]
244+ public static async Task < TDataType ? > ExecuteScalarAsync < TDataType > ( this DbCommand command , TDataType ? defaultValue = default , int retries = 0 )
250245 {
251246 if ( command is null )
252247 return defaultValue ;
253- command . Open ( retries ) ;
248+ _ = command . Open ( retries ) ;
254249 var ReturnValue = await command . ExecuteScalarAsync ( ) . ConfigureAwait ( false ) ;
255250 return ReturnValue . To ( defaultValue ) ;
256251 }
@@ -263,15 +258,14 @@ public static async Task<TDataType> ExecuteScalarAsync<TDataType>(this DbCommand
263258 /// <returns>The DbParameter associated with the ID</returns>
264259 public static DbParameter GetOrCreateParameter ( this DbCommand command , string id )
265260 {
266- if ( command is null )
267- throw new ArgumentNullException ( nameof ( command ) ) ;
261+ ArgumentNullException . ThrowIfNull ( command , nameof ( command ) ) ;
268262 if ( command . Parameters . Contains ( id ) )
269263 {
270264 return command . Parameters [ id ] ;
271265 }
272- var Parameter = command . CreateParameter ( ) ;
266+ DbParameter Parameter = command . CreateParameter ( ) ;
273267 Parameter . ParameterName = id ;
274- command . Parameters . Add ( Parameter ) ;
268+ _ = command . Parameters . Add ( Parameter ) ;
275269 return Parameter ;
276270 }
277271
@@ -286,9 +280,9 @@ public static DbParameter GetOrCreateParameter(this DbCommand command, string id
286280 /// if the parameter exists (and isn't null or empty), it returns the parameter's value.
287281 /// Otherwise the default value is returned.
288282 /// </returns>
289- public static TDataType GetOutputParameter < TDataType > ( this DbCommand command , string id , TDataType defaultValue = default )
283+ public static TDataType ? GetOutputParameter < TDataType > ( this DbCommand command , string id , TDataType ? defaultValue = default )
290284 {
291- return ! ( command ? . Parameters [ id ] is null ) ?
285+ return command ? . Parameters [ id ] is not null ?
292286 command . Parameters [ id ] . Value . To ( defaultValue ) :
293287 defaultValue ;
294288 }
@@ -301,26 +295,28 @@ public static TDataType GetOutputParameter<TDataType>(this DbCommand command, st
301295 /// <returns>The DBCommand object</returns>
302296 public static DbCommand ? Open ( this DbCommand command , int retries = 0 )
303297 {
304- Exception ? holder = null ;
298+ Exception ? Holder = null ;
305299 while ( retries >= 0 )
306300 {
307301 try
308302 {
309- if ( ! ( command ? . Connection is null )
303+ if ( command ? . Connection is not null
310304 && command . Connection . State != ConnectionState . Open )
311305 {
312306 command . Connection . Open ( ) ;
313307 }
314308
315309 return command ;
316310 }
317- catch ( Exception e )
311+ catch ( Exception E )
318312 {
319- holder = e ;
313+ Holder = E ;
320314 }
321315 -- retries ;
322316 }
323- throw holder ! ;
317+ if ( Holder is not null )
318+ throw Holder ;
319+ return command ;
324320 }
325321
326322 /// <summary>
0 commit comments