Created
December 15, 2017 04:58
-
-
Save leachdaniel/d0320ff2ca8ff8cc0022d78060b74575 to your computer and use it in GitHub Desktop.
IDataReader Wrapper that adds some functionality
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
internal class ExtendedDataRecord : IDataRecord | |
{ | |
public ExtendedDataRecord(IDataReader reader) | |
{ | |
_reader = reader; | |
} | |
public int IndexOf(string name) | |
{ | |
if (_fieldNames == null) | |
{ | |
_fieldNames = new Dictionary<string, int>(StringComparer.InvariantCultureIgnoreCase); | |
for (int i = 0; i < _reader.FieldCount; i++) | |
{ | |
_fieldNames.Add(_reader.GetName(i), i); | |
} | |
} | |
if (_fieldNames.TryGetValue(name, out int o)) | |
{ | |
return o; | |
} | |
return -1; | |
} | |
public TValue TryGetValue<TValue>(string name) | |
{ | |
int ordinal = IndexOf(name); | |
if (ordinal != -1) | |
{ | |
object o = GetValue(ordinal); | |
if (o != DBNull.Value) return (TValue)o; | |
} | |
return default(TValue); | |
} | |
private readonly IDataReader _reader; | |
private Dictionary<string, int> _fieldNames = null; | |
public object this[int i] => _reader[i]; | |
public object this[string name] => _reader[name]; | |
public int FieldCount => _reader.FieldCount; | |
public bool GetBoolean(int i) => _reader.GetBoolean(i); | |
public byte GetByte(int i) => _reader.GetByte(i); | |
public long GetBytes(int i, long fieldOffset, byte[] buffer, int bufferoffset, int length) => _reader.GetBytes(i, fieldOffset, buffer, bufferoffset, length); | |
public char GetChar(int i) => _reader.GetChar(i); | |
public long GetChars(int i, long fieldoffset, char[] buffer, int bufferoffset, int length) => _reader.GetChars(i, fieldoffset, buffer, bufferoffset, length); | |
public IDataReader GetData(int i) => _reader.GetData(i); | |
public string GetDataTypeName(int i) => _reader.GetDataTypeName(i); | |
public DateTime GetDateTime(int i) => _reader.GetDateTime(i); | |
public decimal GetDecimal(int i) => _reader.GetDecimal(i); | |
public double GetDouble(int i) => _reader.GetDouble(i); | |
public Type GetFieldType(int i) => _reader.GetFieldType(i); | |
public float GetFloat(int i) => _reader.GetFloat(i); | |
public Guid GetGuid(int i) => _reader.GetGuid(i); | |
public short GetInt16(int i) => _reader.GetInt16(i); | |
public int GetInt32(int i) => _reader.GetInt32(i); | |
public long GetInt64(int i) => _reader.GetInt64(i); | |
public string GetName(int i) => _reader.GetName(i); | |
public int GetOrdinal(string name) => _reader.GetOrdinal(name); | |
public string GetString(int i) => _reader.GetString(i); | |
public object GetValue(int i) => _reader.GetValue(i); | |
public int GetValues(object[] values) => _reader.GetValues(values); | |
public bool IsDBNull(int i) => _reader.IsDBNull(i); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment