diff --git a/src/FluentCommand/DataMapping.cs b/src/FluentCommand/DataMapping.cs index 663bb79c..74d8cbab 100644 --- a/src/FluentCommand/DataMapping.cs +++ b/src/FluentCommand/DataMapping.cs @@ -49,7 +49,6 @@ static DataMapping() {DbType.Boolean, typeof(bool)}, {DbType.Byte, typeof(byte)}, {DbType.Currency, typeof(decimal)}, - {DbType.Date, typeof(DateTime)}, {DbType.DateTime, typeof(DateTime)}, {DbType.Decimal, typeof(decimal)}, {DbType.Double, typeof(double)}, @@ -62,13 +61,20 @@ static DataMapping() {DbType.Single, typeof(float)}, {DbType.String, typeof(string)}, {DbType.StringFixedLength, typeof(string)}, - {DbType.Time, typeof(TimeSpan)}, {DbType.UInt16, typeof(ushort)}, {DbType.UInt32, typeof(uint)}, {DbType.UInt64, typeof(ulong)}, {DbType.VarNumeric, typeof(decimal)}, {DbType.DateTime2, typeof(DateTime)}, {DbType.DateTimeOffset, typeof(DateTimeOffset)}, + #if NET6_0_OR_GREATER + {DbType.Date, typeof(DateOnly)}, + {DbType.Time, typeof(TimeOnly)}, + #else + {DbType.Date, typeof(DateTime)}, + {DbType.Time, typeof(TimeSpan)}, + #endif + }; } diff --git a/src/FluentCommand/Extensions/DataRecordExtensions.cs b/src/FluentCommand/Extensions/DataRecordExtensions.cs index 8d8c2b60..83dd9338 100644 --- a/src/FluentCommand/Extensions/DataRecordExtensions.cs +++ b/src/FluentCommand/Extensions/DataRecordExtensions.cs @@ -1,3 +1,4 @@ +using System; using System.Data; using System.Data.Common; @@ -428,6 +429,76 @@ public static string GetStringNull(this IDataRecord dataRecord, string name) return dataRecord.IsDBNull(ordinal) ? null : dataRecord.GetString(ordinal); } +#if NET6_0_OR_GREATER + /// Gets the value of the specified field. + /// The data record. + /// The of the field to find. + /// The value of the specified field. + public static DateOnly GetDateOnly(this IDataRecord dataRecord, string name) + { + int ordinal = dataRecord.GetOrdinal(name); + if (dataRecord.IsDBNull(ordinal)) + return default; + + if (dataRecord is DbDataReader dataReader) + return dataReader.GetFieldValue(ordinal); + + var dateTime = dataRecord.GetDateTime(ordinal); + return DateOnly.FromDateTime(dateTime); + } + + /// Gets the value of the specified field. + /// The data record. + /// The of the field to find. + /// The value of the specified field. + public static DateOnly? GetDateOnlyNull(this IDataRecord dataRecord, string name) + { + int ordinal = dataRecord.GetOrdinal(name); + if (dataRecord.IsDBNull(ordinal)) + return null; + + if (dataRecord is DbDataReader dataReader) + return dataReader.GetFieldValue(ordinal); + + var dateTime = dataRecord.GetDateTime(ordinal); + return DateOnly.FromDateTime(dateTime); + } + + /// Gets the value of the specified field. + /// The data record. + /// The of the field to find. + /// The value of the specified field. + public static TimeOnly GetTimeOnly(this IDataRecord dataRecord, string name) + { + int ordinal = dataRecord.GetOrdinal(name); + if (dataRecord.IsDBNull(ordinal)) + return default; + + if (dataRecord is DbDataReader dataReader) + return dataReader.GetFieldValue(ordinal); + + var dateTime = dataRecord.GetDateTime(ordinal); + return TimeOnly.FromDateTime(dateTime); + } + + /// Gets the value of the specified field. + /// The data record. + /// The of the field to find. + /// The value of the specified field. + public static TimeOnly? GetTimeOnlyNull(this IDataRecord dataRecord, string name) + { + int ordinal = dataRecord.GetOrdinal(name); + if (dataRecord.IsDBNull(ordinal)) + return null; + + if (dataRecord is DbDataReader dataReader) + return dataReader.GetFieldValue(ordinal); + + var dateTime = dataRecord.GetDateTime(ordinal); + return TimeOnly.FromDateTime(dateTime); + } +#endif + /// Gets the value of the specified field. /// The data record. /// The of the field to find.