Some data reader extensions. Started off with DateOnly
TimeOnly
and added several others
Last active
April 22, 2024 13:56
-
-
Save karenpayneoregon/8e760098053dfafe748c26226cd72a2b to your computer and use it in GitHub Desktop.
SqlDataReader extensions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.Data.SqlClient; | |
internal static class ProviderExtensions | |
{ | |
public static DateOnly GetDateOnly(this SqlDataReader reader, int index) | |
=> reader.GetFieldValue<DateOnly>(index); | |
public static async Task<DateOnly> GetDateOnlyAsync(this SqlDataReader reader, int index) | |
=> await reader.GetFieldValueAsync<DateOnly>(index); | |
public static async Task<DateTime> GetDateTimeAsync(this SqlDataReader reader, int index) | |
=> await reader.GetFieldValueAsync<DateTime>(index); | |
public static async Task<DateTimeOffset> GetDateTimeOffsetAsync(this SqlDataReader reader, int index) | |
=> await reader.GetFieldValueAsync<DateTimeOffset>(index); | |
public static async Task<string> GetStringAsync(this SqlDataReader reader, int index) | |
=> await reader.GetFieldValueAsync<string>(index); | |
public static async Task<int> GetIntAsync(this SqlDataReader reader, int index) | |
=> await reader.GetFieldValueAsync<int>(index); | |
public static async Task<decimal> GetDecimalAsync(this SqlDataReader reader, int index) | |
=> await reader.GetFieldValueAsync<decimal>(index); | |
public static async Task<double> GetDoubleAsync(this SqlDataReader reader, int index) | |
=> await reader.GetFieldValueAsync<double>(index); | |
public static TimeOnly ToTimeOnly(this TimeSpan sender) | |
=> TimeOnly.FromTimeSpan(sender); | |
public static TimeOnly GetTimeOnly(this SqlDataReader reader, int index) | |
=> reader.GetFieldValue<TimeOnly>(index); | |
public static async Task<TimeOnly> GetTimeOnlyAsync(this SqlDataReader reader, int index) | |
=> await reader.GetFieldValueAsync<TimeOnly>(index); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment