@@ -178,6 +178,89 @@ public void Command_Reuse_ShouldWork()
178
178
}
179
179
}
180
180
181
+ [ TestCaseSource ( nameof ( ReUseCommandTypeData ) ) ]
182
+ public void Command_Reuse_ShouldWork_For_Null_Value ( System . Data . DbType dbType , Type type , object value )
183
+ {
184
+ using ( var connection = GetConnection ( ) )
185
+ {
186
+ connection . Open ( ) ;
187
+ using ( var cmd = connection . CreateCommand ( ) )
188
+ {
189
+ var sql = @"SELECT @value" ;
190
+ var p = cmd . CreateParameter ( ) ;
191
+ p . ParameterName = "@value" ;
192
+ p . DbType = dbType ;
193
+ p . Value = Convert . ChangeType ( value , type ) ;
194
+ cmd . CommandText = sql ;
195
+ cmd . Parameters . Add ( p ) ;
196
+ cmd . ExecuteScalar ( ) ;
197
+
198
+ p = cmd . CreateParameter ( ) ;
199
+ p . ParameterName = "@value" ;
200
+ p . DbType = dbType ;
201
+ p . Value = null ;
202
+ cmd . Parameters . Clear ( ) ;
203
+ cmd . Parameters . Add ( p ) ;
204
+
205
+ cmd . ExecuteScalar ( ) ;
206
+ }
207
+ }
208
+ }
209
+
210
+
211
+ [ TestCaseSource ( nameof ( ReUseCommandTypeData ) ) ]
212
+ public void Command_Reuse_ShouldWork_For_NonNull_Value ( System . Data . DbType dbType , Type type , object value )
213
+ {
214
+ using ( var connection = GetConnection ( ) )
215
+ {
216
+ connection . Open ( ) ;
217
+ using ( var cmd = connection . CreateCommand ( ) )
218
+ {
219
+ var sql = @"SELECT @value" ;
220
+ var p = cmd . CreateParameter ( ) ;
221
+ p . ParameterName = "@value" ;
222
+ p . DbType = dbType ;
223
+ p . Value = null ;
224
+ cmd . CommandText = sql ;
225
+ cmd . Parameters . Add ( p ) ;
226
+ cmd . ExecuteScalar ( ) ;
227
+
228
+ p = cmd . CreateParameter ( ) ;
229
+ p . ParameterName = "@value" ;
230
+ p . DbType = dbType ;
231
+ p . Value = Convert . ChangeType ( value , type ) ;
232
+ cmd . Parameters . Clear ( ) ;
233
+ cmd . Parameters . Add ( p ) ;
234
+
235
+ cmd . ExecuteScalar ( ) ;
236
+ }
237
+ }
238
+ }
239
+ private static IEnumerable < TestCaseData > ReUseCommandTypeData ( )
240
+ {
241
+ yield return new TestCaseData ( System . Data . DbType . Boolean , typeof ( bool ) , false ) ;
242
+ yield return new TestCaseData ( System . Data . DbType . Byte , typeof ( byte ) , 12 ) ;
243
+ yield return new TestCaseData ( System . Data . DbType . SByte , typeof ( sbyte ) , 2 ) ;
244
+ yield return new TestCaseData ( System . Data . DbType . Int16 , typeof ( short ) , 3 ) ;
245
+ yield return new TestCaseData ( System . Data . DbType . UInt16 , typeof ( ushort ) , 4 ) ;
246
+ yield return new TestCaseData ( System . Data . DbType . Int32 , typeof ( int ) , 23 ) ;
247
+ yield return new TestCaseData ( System . Data . DbType . UInt32 , typeof ( uint ) , 45 ) ;
248
+ yield return new TestCaseData ( System . Data . DbType . Int64 , typeof ( long ) , 76434755 ) ;
249
+ yield return new TestCaseData ( System . Data . DbType . UInt64 , typeof ( ulong ) , 1223 ) ;
250
+ yield return new TestCaseData ( System . Data . DbType . String , typeof ( string ) , "If it could only be like this always—always summer, always alone, the fruit always ripe" ) ;
251
+ yield return new TestCaseData ( System . Data . DbType . AnsiString , typeof ( string ) , "Doubt thou the stars are fire; Doubt that the sun doth move; Doubt truth to be a liar; But never doubt I love" ) ;
252
+ yield return new TestCaseData ( System . Data . DbType . AnsiStringFixedLength , typeof ( string ) , "For never was a story of more woe than this of Juliet and her Romeo." ) ;
253
+ yield return new TestCaseData ( System . Data . DbType . Guid , typeof ( string ) , "e2207b47-3fce-4187-808f-e206398a9133" ) ;
254
+ yield return new TestCaseData ( System . Data . DbType . Decimal , typeof ( decimal ) , 342.23 ) ;
255
+ yield return new TestCaseData ( System . Data . DbType . Currency , typeof ( decimal ) , 1233.3 ) ;
256
+ yield return new TestCaseData ( System . Data . DbType . Single , typeof ( float ) , 20.34f ) ;
257
+ yield return new TestCaseData ( System . Data . DbType . Double , typeof ( double ) , 3423.234d ) ;
258
+ yield return new TestCaseData ( System . Data . DbType . DateTime , typeof ( DateTime ) , "2019-03-13 03:20:35.23 AM" ) ;
259
+ yield return new TestCaseData ( System . Data . DbType . Date , typeof ( DateTime ) , "2018-07-04 23:20:35.23 PM" ) ;
260
+ yield return new TestCaseData ( System . Data . DbType . Time , typeof ( DateTime ) , "2014-09-10 23:20:35" ) ;
261
+
262
+ }
263
+
181
264
private static IEnumerable < TestCaseData > GetDataTypeName_ShouldWork_Cases ( )
182
265
{
183
266
yield return new TestCaseData ( "convert(unichar(2), 'À')" , "unichar" ) ;
@@ -244,5 +327,15 @@ private static IEnumerable<TestCaseData> GetDataTypeName_ShouldWorkUtf8_Cases()
244
327
yield return new TestCaseData ( "convert(nvarchar(2), 'a')" , "nvarchar" ) ;
245
328
yield return new TestCaseData ( "convert(nvarchar(2), null)" , "nvarchar" ) ;
246
329
}
330
+
331
+ public T CastObject < T > ( object input ) {
332
+ return ( T ) input ;
333
+ }
334
+
335
+ public T ConvertObject < T > ( object input ) {
336
+ return ( T ) Convert . ChangeType ( input , typeof ( T ) ) ;
337
+ }
338
+
247
339
}
340
+
248
341
}
0 commit comments