• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

loresoft / FluentCommand / 10306769181

08 Aug 2024 05:30PM UTC coverage: 51.388%. First build
10306769181

push

github

pwelter34
Update DataRecordExtensions.cs

1519 of 3660 branches covered (41.5%)

Branch coverage included in aggregate %.

1 of 4 new or added lines in 1 file covered. (25.0%)

4126 of 7325 relevant lines covered (56.33%)

226.84 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

29.44
/src/FluentCommand/Extensions/DataRecordExtensions.cs
1
using System.Data;
2
using System.Data.Common;
3

4
namespace FluentCommand.Extensions;
5

6
/// <summary>
7
/// Extension methods for <see cref="IDataRecord"/>.
8
/// </summary>
9
public static class DataRecordExtensions
10
{
11
    /// <summary>Gets the value of the specified column as a <see cref="bool"/>.</summary>
12
    /// <param name="dataRecord">The data record.</param>
13
    /// <param name="name">The <paramref name="name"/> of the field to find.</param>
14
    /// <returns>The value of the column.</returns>
15
    /// <remarks>Returns <see langword="false"/> for <see langword="null"/>.</remarks>
16
    public static bool GetBoolean(this IDataRecord dataRecord, string name)
17
    {
18
        int ordinal = dataRecord.GetOrdinal(name);
135✔
19
        return !dataRecord.IsDBNull(ordinal) && dataRecord.GetBoolean(ordinal);
135!
20
    }
21

22
    /// <summary>Gets the value of the specified column as a <see cref="bool"/>.</summary>
23
    /// <param name="dataRecord">The data record.</param>
24
    /// <param name="name">The <paramref name="name"/> of the field to find.</param>
25
    /// <returns>The value of the column.</returns>
26
    /// <remarks>Returns <see langword="false"/> for <see langword="null"/>.</remarks>
27
    public static bool? GetBooleanNull(this IDataRecord dataRecord, string name)
28
    {
29
        int ordinal = dataRecord.GetOrdinal(name);
×
30
        return !dataRecord.IsDBNull(ordinal) ? null : dataRecord.GetBoolean(ordinal);
×
31
    }
32

33
    /// <summary>Gets the 8-bit unsigned integer value of the specified column.</summary>
34
    /// <param name="dataRecord">The data record.</param>
35
    /// <param name="name">The <paramref name="name"/> of the field to find.</param>
36
    /// <returns>The 8-bit unsigned integer value of the specified column.</returns>
37
    /// <remarks>Returns 0 for <see langword="null"/>.</remarks>
38
    public static byte GetByte(this IDataRecord dataRecord, string name)
39
    {
40
        int ordinal = dataRecord.GetOrdinal(name);
×
41
        return dataRecord.IsDBNull(ordinal) ? (byte)0 : dataRecord.GetByte(ordinal);
×
42
    }
43

44
    /// <summary>Gets the 8-bit unsigned integer value of the specified column.</summary>
45
    /// <param name="dataRecord">The data record.</param>
46
    /// <param name="name">The <paramref name="name"/> of the field to find.</param>
47
    /// <returns>The 8-bit unsigned integer value of the specified column.</returns>
48
    /// <remarks>Returns 0 for <see langword="null"/>.</remarks>
49
    public static byte? GetByteNull(this IDataRecord dataRecord, string name)
50
    {
51
        int ordinal = dataRecord.GetOrdinal(name);
×
52
        return dataRecord.IsDBNull(ordinal) ? null : dataRecord.GetByte(ordinal);
×
53
    }
54

55
    /// <summary>Gets a stream of bytes from the  specified column.</summary>
56
    /// <param name="dataRecord">The data record.</param>
57
    /// <param name="name">The <paramref name="name"/> of the field to find.</param>
58
    /// <returns>A stream of bytes of the specified column.</returns>
59
    /// <remarks>Returns empty array for <see langword="null"/>.</remarks>
60
    public static byte[] GetBytes(this IDataRecord dataRecord, string name)
61
    {
62
        int ordinal = dataRecord.GetOrdinal(name);
45✔
63
        if (dataRecord.IsDBNull(ordinal))
45✔
64
            return Array.Empty<byte>();
30✔
65

66
        return GetBytes(dataRecord, ordinal);
15✔
67
    }
68

69
    /// <summary>Gets a stream of bytes from the  specified column.</summary>
70
    /// <param name="dataRecord">The data record.</param>
71
    /// <param name="index">The zero-based column ordinal.</param>
72
    /// <returns>A stream of bytes of the specified column.</returns>
73
    public static byte[] GetBytes(this IDataRecord dataRecord, int index)
74
    {
75
        //get the length of data
76
        long size = dataRecord.GetBytes(index, 0, null, 0, 0);
1,197✔
77
        byte[] buffer = new byte[size];
1,197✔
78

79
        int bufferSize = size <= 1024 ? (int)size : 1024;
1,197!
80
        long bytesRead = 0;
1,197✔
81
        int offset = 0;
1,197✔
82

83
        while (bytesRead < size)
2,394✔
84
        {
85
            bytesRead += dataRecord.GetBytes(index, offset, buffer, offset, bufferSize);
1,197✔
86
            offset += bufferSize;
1,197✔
87
        }
88

89
        return buffer;
1,197✔
90

91
    }
92

93
    /// <summary>
94
    /// Reads a stream of bytes from the specified column offset into the buffer as an array, starting at the given buffer offset.
95
    /// </summary>
96
    /// <param name="dataRecord">The data record.</param>
97
    /// <param name="name">The <paramref name="name"/> of the field to find.</param>
98
    /// <param name="fieldOffset">The index within the field from which to start the read operation.</param>
99
    /// <param name="buffer">The buffer into which to read the stream of bytes.</param>
100
    /// <param name="bufferOffset">The index for buffer to start the read operation.</param>
101
    /// <param name="length">The number of bytes to read.</param>
102
    /// <returns>The actual number of bytes read.</returns>
103
    /// <remarks>Returns 0 for <see langword="null"/>.</remarks>
104
    public static long GetBytes(this IDataRecord dataRecord, string name, long fieldOffset, byte[] buffer, int bufferOffset, int length)
105
    {
106
        int ordinal = dataRecord.GetOrdinal(name);
×
107
        return dataRecord.IsDBNull(ordinal) ? 0 : dataRecord.GetBytes(dataRecord.GetOrdinal(name), fieldOffset, buffer, bufferOffset, length);
×
108
    }
109

110
    /// <summary>Gets the character value of the specified column.</summary>
111
    /// <param name="dataRecord">The data record.</param>
112
    /// <param name="name">The <paramref name="name"/> of the field to find.</param>
113
    /// <returns>The character value of the specified column.</returns>
114
    /// <remarks>Returns Char.MinValue for <see langword="null"/>.</remarks>
115
    public static char GetChar(this IDataRecord dataRecord, string name)
116
    {
117
        int ordinal = dataRecord.GetOrdinal(name);
×
118
        return dataRecord.IsDBNull(ordinal) ? char.MinValue : dataRecord.GetChar(ordinal);
×
119
    }
120

121
    /// <summary>Gets the character value of the specified column.</summary>
122
    /// <param name="dataRecord">The data record.</param>
123
    /// <param name="name">The <paramref name="name"/> of the field to find.</param>
124
    /// <returns>The character value of the specified column.</returns>
125
    /// <remarks>Returns Char.MinValue for <see langword="null"/>.</remarks>
126
    public static char? GetCharNull(this IDataRecord dataRecord, string name)
127
    {
128
        int ordinal = dataRecord.GetOrdinal(name);
×
129
        return dataRecord.IsDBNull(ordinal) ? null : dataRecord.GetChar(ordinal);
×
130
    }
131

132
    /// <summary>
133
    /// Reads a stream of characters from the specified column offset into the buffer as an array, starting at the given buffer offset.
134
    /// </summary>
135
    /// <param name="dataRecord">The data record.</param>
136
    /// <param name="name">The <paramref name="name"/> of the field to find.</param>
137
    /// <param name="fieldOffset">The index within the row from which to start the read operation.</param>
138
    /// <param name="buffer">The buffer into which to read the stream of bytes.</param>
139
    /// <param name="bufferOffset">The index for buffer to start the read operation. </param>
140
    /// <param name="length">The number of bytes to read.</param>
141
    /// <returns>The actual number of characters read.</returns>
142
    /// <remarks>Returns 0 for <see langword="null"/>.</remarks>
143
    public static long GetChars(this IDataRecord dataRecord, string name, long fieldOffset, char[] buffer, int bufferOffset, int length)
144
    {
145
        int ordinal = dataRecord.GetOrdinal(name);
×
146
        return dataRecord.IsDBNull(ordinal) ? 0 : dataRecord.GetChars(dataRecord.GetOrdinal(name), fieldOffset, buffer, bufferOffset, length);
×
147
    }
148

149
    /// <summary>
150
    /// Returns an <see cref="IDataReader"/> for the specified column <paramref name="name"/>.
151
    /// </summary>
152
    /// <param name="dataRecord">The data record.</param>
153
    /// <param name="name">The <paramref name="name"/> of the field to find.</param>
154
    /// <returns>The <see cref="IDataReader"/> for the specified column <paramref name="name"/>.</returns>
155
    public static IDataReader GetData(this IDataRecord dataRecord, string name)
156
    {
157
        int ordinal = dataRecord.GetOrdinal(name);
×
158
        return dataRecord.IsDBNull(ordinal) ? null : dataRecord.GetData(ordinal);
×
159
    }
160

161
    /// <summary>Gets the data type information for the specified field.</summary>
162
    /// <param name="dataRecord">The data record.</param>
163
    /// <param name="name">The <paramref name="name"/> of the field to find.</param>
164
    /// <returns>The data type information for the specified field.</returns>
165
    public static string GetDataTypeName(this IDataRecord dataRecord, string name)
166
    {
167
        int ordinal = dataRecord.GetOrdinal(name);
×
168
        return dataRecord.GetDataTypeName(ordinal);
×
169
    }
170

171
    /// <summary>Gets the date and time data value of the specified field.</summary>
172
    /// <param name="dataRecord">The data record.</param>
173
    /// <param name="name">The <paramref name="name"/> of the field to find.</param>
174
    /// <returns>The date and time data value of the specified field.</returns>
175
    /// <remarks>Returns DateTime.MinValue for <see langword="null"/>.</remarks>
176
    public static DateTime GetDateTime(this IDataRecord dataRecord, string name)
177
    {
178
        int ordinal = dataRecord.GetOrdinal(name);
×
179
        return dataRecord.IsDBNull(ordinal) ? DateTime.MinValue : dataRecord.GetDateTime(ordinal);
×
180
    }
181

182
    /// <summary>Gets the date and time data value of the specified field.</summary>
183
    /// <param name="dataRecord">The data record.</param>
184
    /// <param name="name">The <paramref name="name"/> of the field to find.</param>
185
    /// <returns>The date and time data value of the specified field.</returns>
186
    /// <remarks>Returns DateTime.MinValue for <see langword="null"/>.</remarks>
187
    public static DateTime? GetDateTimeNull(this IDataRecord dataRecord, string name)
188
    {
189
        int ordinal = dataRecord.GetOrdinal(name);
×
190
        return dataRecord.IsDBNull(ordinal) ? null : dataRecord.GetDateTime(ordinal);
×
191
    }
192

193
    /// <summary>Gets the date and time data value of the specified field.</summary>
194
    /// <param name="dataRecord">The data record.</param>
195
    /// <param name="name">The <paramref name="name"/> of the field to find.</param>
196
    /// <returns>The date and time data value of the specified field.</returns>
197
    /// <remarks>Returns DateTime.MinValue for <see langword="null"/>.</remarks>
198
    public static DateTimeOffset GetDateTimeOffset(this IDataRecord dataRecord, string name)
199
    {
200
        int ordinal = dataRecord.GetOrdinal(name);
90✔
201
        if (dataRecord.IsDBNull(ordinal))
90!
202
            return DateTimeOffset.MinValue;
×
203

204
        return GetDateTimeOffset(dataRecord, ordinal);
90✔
205
    }
206

207
    /// <summary>Gets the date and time data value of the specified field.</summary>
208
    /// <param name="dataRecord">The data record.</param>
209
    /// <param name="name">The <paramref name="name"/> of the field to find.</param>
210
    /// <returns>The date and time data value of the specified field.</returns>
211
    /// <remarks>Returns DateTime.MinValue for <see langword="null"/>.</remarks>
212
    public static DateTimeOffset? GetDateTimeOffsetNull(this IDataRecord dataRecord, string name)
213
    {
214
        int ordinal = dataRecord.GetOrdinal(name);
90✔
215
        if (dataRecord.IsDBNull(ordinal))
90!
216
            return null;
90✔
217

218
        return GetDateTimeOffset(dataRecord, ordinal);
×
219
    }
220

221
    /// <summary>Gets the date and time data value of the specified field.</summary>
222
    /// <param name="dataRecord">The data record.</param>
223
    /// <param name="index">The zero-based column ordinal.</param>
224
    /// <returns>The date and time data value of the specified field.</returns>
225
    public static DateTimeOffset GetDateTimeOffset(this IDataRecord dataRecord, int index)
226
    {
227
        if (dataRecord is DbDataReader dataReader)
2,622!
228
            return dataReader.GetFieldValue<DateTimeOffset>(index);
2,622✔
229

230
        var value = dataRecord.GetValue(index);
×
231
        if (value is DateTimeOffset offset)
×
232
            return offset;
×
233

234
        var date = dataRecord.GetDateTime(index);
×
235
        date = DateTime.SpecifyKind(date, DateTimeKind.Utc);
×
236

237
        return new DateTimeOffset(date, TimeSpan.Zero);
×
238
    }
239

240
    /// <summary>Gets the fixed-position numeric value of the specified field.</summary>
241
    /// <param name="dataRecord">The data record.</param>
242
    /// <param name="name">The <paramref name="name"/> of the field to find.</param>
243
    /// <returns>The fixed-position numeric value of the specified field.</returns>
244
    /// <remarks>Returns 0 for <see langword="null"/>.</remarks>
245
    public static decimal GetDecimal(this IDataRecord dataRecord, string name)
246
    {
247
        int ordinal = dataRecord.GetOrdinal(name);
×
248
        return dataRecord.IsDBNull(ordinal) ? 0 : dataRecord.GetDecimal(ordinal);
×
249
    }
250

251
    /// <summary>Gets the fixed-position numeric value of the specified field.</summary>
252
    /// <param name="dataRecord">The data record.</param>
253
    /// <param name="name">The <paramref name="name"/> of the field to find.</param>
254
    /// <returns>The fixed-position numeric value of the specified field.</returns>
255
    /// <remarks>Returns 0 for <see langword="null"/>.</remarks>
256
    public static decimal? GetDecimalNull(this IDataRecord dataRecord, string name)
257
    {
258
        int ordinal = dataRecord.GetOrdinal(name);
×
259
        return dataRecord.IsDBNull(ordinal) ? null : dataRecord.GetDecimal(ordinal);
×
260
    }
261

262
    /// <summary>Gets the double-precision floating point number of the specified field.</summary>
263
    /// <param name="dataRecord">The data record.</param>
264
    /// <param name="name">The <paramref name="name"/> of the field to find.</param>
265
    /// <returns>The double-precision floating point number of the specified field.</returns>
266
    /// <remarks>Returns 0 for <see langword="null"/>.</remarks>
267
    public static double GetDouble(this IDataRecord dataRecord, string name)
268
    {
269
        int ordinal = dataRecord.GetOrdinal(name);
×
270
        return dataRecord.IsDBNull(ordinal) ? 0D : dataRecord.GetDouble(ordinal);
×
271
    }
272

273
    /// <summary>Gets the double-precision floating point number of the specified field.</summary>
274
    /// <param name="dataRecord">The data record.</param>
275
    /// <param name="name">The <paramref name="name"/> of the field to find.</param>
276
    /// <returns>The double-precision floating point number of the specified field.</returns>
277
    /// <remarks>Returns 0 for <see langword="null"/>.</remarks>
278
    public static double? GetDoubleNull(this IDataRecord dataRecord, string name)
279
    {
280
        int ordinal = dataRecord.GetOrdinal(name);
×
281
        return dataRecord.IsDBNull(ordinal) ? null : dataRecord.GetDouble(ordinal);
×
282
    }
283

284
    /// <summary>
285
    /// Gets the <see cref="Type"/> information corresponding to the type of <see cref="object"/> that would be returned from <see cref="GetValue"/>.
286
    /// </summary>
287
    /// <param name="dataRecord">The data record.</param>
288
    /// <param name="name">The <paramref name="name"/> of the field to find.</param>
289
    /// <returns>The <see cref="Type"/> information corresponding to the type of <see cref="object"/> that would be returned from <see cref="GetValue"/>.</returns>
290
    public static Type GetFieldType(this IDataRecord dataRecord, string name)
291
    {
292
        int ordinal = dataRecord.GetOrdinal(name);
1,200✔
293
        return dataRecord.GetFieldType(ordinal);
1,200✔
294
    }
295

296
    /// <summary>Gets the single-precision floating point number of the specified field.</summary>
297
    /// <param name="dataRecord">The data record.</param>
298
    /// <param name="name">The <paramref name="name"/> of the field to find.</param>
299
    /// <returns>The single-precision floating point number of the specified field.</returns>
300
    /// <remarks>Returns 0 for <see langword="null"/>.</remarks>
301
    public static float GetFloat(this IDataRecord dataRecord, string name)
302
    {
303
        int ordinal = dataRecord.GetOrdinal(name);
×
304
        return dataRecord.IsDBNull(ordinal) ? 0F : dataRecord.GetFloat(ordinal);
×
305
    }
306

307
    /// <summary>Gets the single-precision floating point number of the specified field.</summary>
308
    /// <param name="dataRecord">The data record.</param>
309
    /// <param name="name">The <paramref name="name"/> of the field to find.</param>
310
    /// <returns>The single-precision floating point number of the specified field.</returns>
311
    /// <remarks>Returns 0 for <see langword="null"/>.</remarks>
312
    public static float? GetFloatNull(this IDataRecord dataRecord, string name)
313
    {
314
        int ordinal = dataRecord.GetOrdinal(name);
×
315
        return dataRecord.IsDBNull(ordinal) ? null : dataRecord.GetFloat(ordinal);
×
316
    }
317

318
    /// <summary>
319
    /// Gets the <see cref="Guid"/> value of the specified field..
320
    /// </summary>
321
    /// <param name="dataRecord">The data record.</param>
322
    /// <param name="name">The <paramref name="name"/> of the field to find.</param>
323
    /// <returns>The <see cref="Guid"/> value of the specified field.</returns>
324
    /// <remarks>Returns Guid.Empty for <see langword="null"/>.</remarks>
325
    public static Guid GetGuid(this IDataRecord dataRecord, string name)
326
    {
327
        int ordinal = dataRecord.GetOrdinal(name);
45✔
328
        return dataRecord.IsDBNull(ordinal) ? Guid.Empty : dataRecord.GetGuid(ordinal);
45!
329
    }
330

331
    /// <summary>
332
    /// Gets the <see cref="Guid"/> value of the specified field..
333
    /// </summary>
334
    /// <param name="dataRecord">The data record.</param>
335
    /// <param name="name">The <paramref name="name"/> of the field to find.</param>
336
    /// <returns>The <see cref="Guid"/> value of the specified field.</returns>
337
    /// <remarks>Returns Guid.Empty for <see langword="null"/>.</remarks>
338
    public static Guid? GetGuidNull(this IDataRecord dataRecord, string name)
339
    {
340
        int ordinal = dataRecord.GetOrdinal(name);
×
341
        return dataRecord.IsDBNull(ordinal) ? null : dataRecord.GetGuid(ordinal);
×
342
    }
343

344
    /// <summary>Gets the 16-bit signed integer value of the specified field.</summary>
345
    /// <param name="dataRecord">The data record.</param>
346
    /// <param name="name">The <paramref name="name"/> of the field to find.</param>
347
    /// <returns>The 16-bit signed integer value of the specified field.</returns>
348
    /// <remarks>Returns 0 for <see langword="null"/>.</remarks>
349
    public static short GetInt16(this IDataRecord dataRecord, string name)
350
    {
351
        int ordinal = dataRecord.GetOrdinal(name);
×
352
        return dataRecord.IsDBNull(ordinal) ? (short)0 : dataRecord.GetInt16(ordinal);
×
353
    }
354

355
    /// <summary>Gets the 16-bit signed integer value of the specified field.</summary>
356
    /// <param name="dataRecord">The data record.</param>
357
    /// <param name="name">The <paramref name="name"/> of the field to find.</param>
358
    /// <returns>The 16-bit signed integer value of the specified field.</returns>
359
    /// <remarks>Returns 0 for <see langword="null"/>.</remarks>
360
    public static short? GetInt16Null(this IDataRecord dataRecord, string name)
361
    {
362
        int ordinal = dataRecord.GetOrdinal(name);
×
363
        return dataRecord.IsDBNull(ordinal) ? null : dataRecord.GetInt16(ordinal);
×
364
    }
365

366
    /// <summary>Gets the 32-bit signed integer value of the specified field.</summary>
367
    /// <param name="dataRecord">The data record.</param>
368
    /// <param name="name">The <paramref name="name"/> of the field to find.</param>
369
    /// <returns>The 32-bit signed integer value of the specified field.</returns>
370
    /// <remarks>Returns 0 for <see langword="null"/>.</remarks>
371
    public static int GetInt32(this IDataRecord dataRecord, string name)
372
    {
373
        int ordinal = dataRecord.GetOrdinal(name);
45✔
374
        return dataRecord.IsDBNull(ordinal) ? 0 : dataRecord.GetInt32(ordinal);
45!
375
    }
376

377
    /// <summary>Gets the 32-bit signed integer value of the specified field.</summary>
378
    /// <param name="dataRecord">The data record.</param>
379
    /// <param name="name">The <paramref name="name"/> of the field to find.</param>
380
    /// <returns>The 32-bit signed integer value of the specified field.</returns>
381
    /// <remarks>Returns 0 for <see langword="null"/>.</remarks>
382
    public static int? GetInt32Null(this IDataRecord dataRecord, string name)
383
    {
384
        int ordinal = dataRecord.GetOrdinal(name);
×
385
        return dataRecord.IsDBNull(ordinal) ? null : dataRecord.GetInt32(ordinal);
×
386
    }
387

388
    /// <summary>Gets the 64-bit signed integer value of the specified field.</summary>
389
    /// <param name="dataRecord">The data record.</param>
390
    /// <param name="name">The <paramref name="name"/> of the field to find.</param>
391
    /// <returns>The 64-bit signed integer value of the specified field.</returns>
392
    /// <remarks>Returns 0 for <see langword="null"/>.</remarks>
393
    public static long GetInt64(this IDataRecord dataRecord, string name)
394
    {
395
        int ordinal = dataRecord.GetOrdinal(name);
×
396
        return dataRecord.IsDBNull(ordinal) ? 0 : dataRecord.GetInt64(ordinal);
×
397
    }
398

399
    /// <summary>Gets the 64-bit signed integer value of the specified field.</summary>
400
    /// <param name="dataRecord">The data record.</param>
401
    /// <param name="name">The <paramref name="name"/> of the field to find.</param>
402
    /// <returns>The 64-bit signed integer value of the specified field.</returns>
403
    /// <remarks>Returns 0 for <see langword="null"/>.</remarks>
404
    public static long? GetInt64Null(this IDataRecord dataRecord, string name)
405
    {
406
        int ordinal = dataRecord.GetOrdinal(name);
×
407
        return dataRecord.IsDBNull(ordinal) ? null : dataRecord.GetInt64(ordinal);
×
408
    }
409

410
    /// <summary>Gets the string value of the specified field.</summary>
411
    /// <param name="dataRecord">The data record.</param>
412
    /// <param name="name">The <paramref name="name"/> of the field to find.</param>
413
    /// <returns>The string value of the specified field.</returns>
414
    /// <remarks>Returns String.Empty for <see langword="null"/>.</remarks>
415
    public static string GetString(this IDataRecord dataRecord, string name)
416
    {
417
        int ordinal = dataRecord.GetOrdinal(name);
415✔
418
        return dataRecord.IsDBNull(ordinal) ? string.Empty : dataRecord.GetString(ordinal);
415✔
419
    }
420

421
    /// <summary>Gets the string value of the specified field.</summary>
422
    /// <param name="dataRecord">The data record.</param>
423
    /// <param name="name">The <paramref name="name"/> of the field to find.</param>
424
    /// <returns>The string value of the specified field.</returns>
425
    public static string GetStringNull(this IDataRecord dataRecord, string name)
426
    {
427
        int ordinal = dataRecord.GetOrdinal(name);
×
428
        return dataRecord.IsDBNull(ordinal) ? null : dataRecord.GetString(ordinal);
×
429
    }
430

431
    /// <summary>Gets the value of the specified field.</summary>
432
    /// <param name="dataRecord">The data record.</param>
433
    /// <param name="name">The <paramref name="name"/> of the field to find.</param>
434
    /// <returns>The <see cref="object"/> which will contain the field value upon return.</returns>
435
    public static object GetValue(this IDataRecord dataRecord, string name)
436
    {
437
        int ordinal = dataRecord.GetOrdinal(name);
2,400✔
438
        return dataRecord.IsDBNull(ordinal) ? null : dataRecord.GetValue(ordinal);
2,400✔
439
    }
440

441
    /// <summary>
442
    /// Gets the value of the specified field.
443
    /// </summary>
444
    /// <typeparam name="T">The record value type</typeparam>
445
    /// <param name="dataRecord">The data record.</param>
446
    /// <param name="name">The <paramref name="name"/> of the field to find.</param>
447
    /// <returns>
448
    /// The <typeparamref name="T"/> which will contain the field value upon return.
449
    /// </returns>
450
    public static T GetValue<T>(this IDataRecord dataRecord, string name)
451
    {
452
        int ordinal = dataRecord.GetOrdinal(name);
×
NEW
453
        if (dataRecord.IsDBNull(ordinal))
×
NEW
454
            return default;
×
455

456
        if (dataRecord is DbDataReader dataReader)
×
457
            return dataReader.GetFieldValue<T>(ordinal);
×
458

459
        return (T)dataRecord.GetValue(ordinal);
×
460
    }
461

462
    /// <summary>
463
    /// Gets the value of the specified field.
464
    /// </summary>
465
    /// <typeparam name="T">The record value type</typeparam>
466
    /// <param name="dataRecord">The data record.</param>
467
    /// <param name="index">The zero-based column ordinal.</param>
468
    /// <returns>
469
    /// The <typeparamref name="T"/> which will contain the field value upon return.
470
    /// </returns>
471
    public static T GetValue<T>(this IDataRecord dataRecord, int index)
472
    {
473
        if (dataRecord.IsDBNull(index))
22!
NEW
474
            return default;
×
475

476
        if (dataRecord is DbDataReader dataReader)
22!
477
            return dataReader.GetFieldValue<T>(index);
22✔
478

479
        return (T)dataRecord.GetValue(index);
×
480
    }
481

482
    /// <summary>Determines whether the specified field is set to <see langword="null"/>.</summary>
483
    /// <param name="dataRecord">The data record.</param>
484
    /// <param name="name">The <paramref name="name"/> of the field to find.</param>
485
    /// <returns><c>true</c> if the specified field is set to <see langword="null"/>; otherwise, <c>false</c>.</returns>
486
    public static bool IsDBNull(this IDataRecord dataRecord, string name)
487
    {
488
        int ordinal = dataRecord.GetOrdinal(name);
×
489
        return dataRecord.IsDBNull(ordinal);
×
490
    }
491
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc