// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. using System.ComponentModel; using System.Data.Common; using System.IO; using System.Threading; using System.Threading.Tasks; namespace System.Data { // copied from CoreFX (NS2.1) public static class DataReaderExtensions { public static bool GetBoolean(this DbDataReader reader, string name) { AssertNotNull(reader); return reader.GetBoolean(reader.GetOrdinal(name)); } public static byte GetByte(this DbDataReader reader, string name) { AssertNotNull(reader); return reader.GetByte(reader.GetOrdinal(name)); } public static long GetBytes(this DbDataReader reader, string name, long dataOffset, byte[] buffer, int bufferOffset, int length) { AssertNotNull(reader); return reader.GetBytes(reader.GetOrdinal(name), dataOffset, buffer, bufferOffset, length); } public static char GetChar(this DbDataReader reader, string name) { AssertNotNull(reader); return reader.GetChar(reader.GetOrdinal(name)); } public static long GetChars(this DbDataReader reader, string name, long dataOffset, char[] buffer, int bufferOffset, int length) { AssertNotNull(reader); return reader.GetChars(reader.GetOrdinal(name), dataOffset, buffer, bufferOffset, length); } [EditorBrowsable(EditorBrowsableState.Never)] public static DbDataReader GetData(this DbDataReader reader, string name) { AssertNotNull(reader); return reader.GetData(reader.GetOrdinal(name)); } public static string GetDataTypeName(this DbDataReader reader, string name) { AssertNotNull(reader); return reader.GetDataTypeName(reader.GetOrdinal(name)); } public static DateTime GetDateTime(this DbDataReader reader, string name) { AssertNotNull(reader); return reader.GetDateTime(reader.GetOrdinal(name)); } public static decimal GetDecimal(this DbDataReader reader, string name) { AssertNotNull(reader); return reader.GetDecimal(reader.GetOrdinal(name)); } public static double GetDouble(this DbDataReader reader, string name) { AssertNotNull(reader); return reader.GetDouble(reader.GetOrdinal(name)); } public static Type GetFieldType(this DbDataReader reader, string name) { AssertNotNull(reader); return reader.GetFieldType(reader.GetOrdinal(name)); } public static T GetFieldValue(this DbDataReader reader, string name) { AssertNotNull(reader); return reader.GetFieldValue(reader.GetOrdinal(name)); } public static Task GetFieldValueAsync(this DbDataReader reader, string name, CancellationToken cancellationToken = default(CancellationToken)) { AssertNotNull(reader); return reader.GetFieldValueAsync(reader.GetOrdinal(name), cancellationToken); } public static float GetFloat(this DbDataReader reader, string name) { AssertNotNull(reader); return reader.GetFloat(reader.GetOrdinal(name)); } public static Guid GetGuid(this DbDataReader reader, string name) { AssertNotNull(reader); return reader.GetGuid(reader.GetOrdinal(name)); } public static short GetInt16(this DbDataReader reader, string name) { AssertNotNull(reader); return reader.GetInt16(reader.GetOrdinal(name)); } public static int GetInt32(this DbDataReader reader, string name) { AssertNotNull(reader); return reader.GetInt32(reader.GetOrdinal(name)); } public static long GetInt64(this DbDataReader reader, string name) { AssertNotNull(reader); return reader.GetInt64(reader.GetOrdinal(name)); } [EditorBrowsable(EditorBrowsableState.Never)] public static Type GetProviderSpecificFieldType(this DbDataReader reader, string name) { AssertNotNull(reader); return reader.GetProviderSpecificFieldType(reader.GetOrdinal(name)); } [EditorBrowsable(EditorBrowsableState.Never)] public static object GetProviderSpecificValue(this DbDataReader reader, string name) { AssertNotNull(reader); return reader.GetProviderSpecificValue(reader.GetOrdinal(name)); } public static Stream GetStream(this DbDataReader reader, string name) { AssertNotNull(reader); return reader.GetStream(reader.GetOrdinal(name)); } public static string GetString(this DbDataReader reader, string name) { AssertNotNull(reader); return reader.GetString(reader.GetOrdinal(name)); } public static TextReader GetTextReader(this DbDataReader reader, string name) { AssertNotNull(reader); return reader.GetTextReader(reader.GetOrdinal(name)); } public static object GetValue(this DbDataReader reader, string name) { AssertNotNull(reader); return reader.GetValue(reader.GetOrdinal(name)); } public static bool IsDBNull(this DbDataReader reader, string name) { AssertNotNull(reader); return reader.IsDBNull(reader.GetOrdinal(name)); } public static Task IsDBNullAsync(this DbDataReader reader, string name, CancellationToken cancellationToken = default(CancellationToken)) { AssertNotNull(reader); return reader.IsDBNullAsync(reader.GetOrdinal(name), cancellationToken); } private static void AssertNotNull(DbDataReader reader) { if (reader == null) { throw new ArgumentNullException(nameof(reader)); } } } }