Skip to content

Commit c42acc5

Browse files
committed
Bug-fix on DBNull in ADO adapter
1 parent 7d51e1d commit c42acc5

File tree

3 files changed

+17
-5
lines changed

3 files changed

+17
-5
lines changed

Simple.Data.Ado/DataReaderExtensions.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using System.Linq;
34
using System.Data;
45
using Simple.Data.Extensions;
@@ -44,7 +45,7 @@ private static IEnumerable<IDictionary<string,object>> ToDictionariesImpl(IDataR
4445
while (reader.Read())
4546
{
4647
reader.GetValues(values);
47-
yield return OptimizedDictionary.Create(index, values);
48+
yield return OptimizedDictionary.Create(index, values.Replace(DBNull.Value, null));
4849
}
4950
}
5051

Simple.Data.Ado/DataRecordExtensions.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using System.Linq;
34
using System.Data;
45
using Simple.Data.Extensions;
@@ -29,7 +30,7 @@ public static dynamic ToDynamicRecord(this IDataRecord dataRecord, IDictionary<s
2930

3031
public static Dictionary<string, object> ToDictionary(this IDataRecord dataRecord)
3132
{
32-
return dataRecord.GetFieldNames().ToDictionary(fieldName => fieldName.Homogenize(), fieldName => dataRecord[fieldName]);
33+
return dataRecord.GetFieldNames().ToDictionary(fieldName => fieldName.Homogenize(), fieldName => DBNullToNull(dataRecord[fieldName]));
3334
}
3435

3536
public static IDictionary<string, object> ToDictionary(this IDataRecord dataRecord, IDictionary<string,int> index)
@@ -49,7 +50,12 @@ public static IEnumerable<object> GetValues(this IDataRecord dataRecord)
4950
{
5051
var values = new object[dataRecord.FieldCount];
5152
dataRecord.GetValues(values);
52-
return values;
53+
return values.Replace(DBNull.Value, null);
54+
}
55+
56+
private static object DBNullToNull(object source)
57+
{
58+
return source == DBNull.Value ? null : source;
5359
}
5460
}
5561
}

Simple.Data/Extensions/EnumerableExtensions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,5 +87,10 @@ public static IEnumerable<T> ExtendInfinite<T>(this IEnumerable<T> source)
8787
yield return default(T);
8888
}
8989
}
90+
91+
public static IEnumerable<T> Replace<T>(this IEnumerable<T> source, T toReplace, T replaceWith)
92+
{
93+
return source.Select(item => Equals(item, toReplace) ? replaceWith : item);
94+
}
9095
}
9196
}

0 commit comments

Comments
 (0)