Skip to content

Instantly share code, notes, and snippets.

@karenpayneoregon
Last active April 22, 2024 13:56
Show Gist options
  • Save karenpayneoregon/8e760098053dfafe748c26226cd72a2b to your computer and use it in GitHub Desktop.
Save karenpayneoregon/8e760098053dfafe748c26226cd72a2b to your computer and use it in GitHub Desktop.
SqlDataReader extensions

Some data reader extensions. Started off with DateOnly ↔️ TimeOnly and added several others

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