diff --git a/src/NReco.Data.Tests/DbDataAdapterTests.cs b/src/NReco.Data.Tests/DbDataAdapterTests.cs index c0dd08d..785f4e6 100644 --- a/src/NReco.Data.Tests/DbDataAdapterTests.cs +++ b/src/NReco.Data.Tests/DbDataAdapterTests.cs @@ -42,6 +42,10 @@ public void Select() { // count for schema-qualified query Assert.Equal(5, DbAdapter.Select( new Query( new QTable( "main.contacts",null) ).Select(QField.Count) ).Single() ); + // select single that is null + Assert.False( DbAdapter.Select( new Query("companies", (QField)"id">(QConst)1000).Select(new QField("sum", "sum(size)")) ).Single().HasValue ); + Assert.Equal(0, DbAdapter.Select(new Query("companies", (QField)"id" > (QConst)1000).Select(new QField("sum", "sum(size)"))).Single() ); + // count: data view Assert.Equal(5, DbAdapter.Select(new Query("contacts_view").Select(QField.Count) ).Single() ); diff --git a/src/NReco.Data/Result/DataReaderResult.cs b/src/NReco.Data/Result/DataReaderResult.cs index c5f7072..2a1be1d 100644 --- a/src/NReco.Data/Result/DataReaderResult.cs +++ b/src/NReco.Data/Result/DataReaderResult.cs @@ -237,6 +237,9 @@ public DataTable ToDataTable(DataTable tbl) { #endif private T ChangeType(object o, TypeCode typeCode) { + if (DataHelper.IsNullOrDBNull(o)) { + return default(T); + } return (T)Convert.ChangeType(o, typeCode, System.Globalization.CultureInfo.InvariantCulture); } @@ -248,7 +251,8 @@ private Dictionary ReadDictionary(IDataReader rdr) { } private T Read(IDataReader rdr) { - var typeCode = Type.GetTypeCode(typeof(T)); + var undelyingType = Nullable.GetUnderlyingType(typeof(T)); + var typeCode = Type.GetTypeCode(undelyingType ?? typeof(T)); // handle primitive single-value result if (typeCode!=TypeCode.Object || typeof(T)==typeof(object)) { if (rdr.FieldCount==1) {