Guest User

Untitled

a guest
Dec 17th, 2017
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. internal class ExtendedDataRecord : IDataRecord
  2. {
  3. public ExtendedDataRecord(IDataReader reader)
  4. {
  5. _reader = reader;
  6. }
  7. public int IndexOf(string name)
  8. {
  9. if (_fieldNames == null)
  10. {
  11. _fieldNames = new Dictionary<string, int>(StringComparer.InvariantCultureIgnoreCase);
  12. for (int i = 0; i < _reader.FieldCount; i++)
  13. {
  14. _fieldNames.Add(_reader.GetName(i), i);
  15. }
  16. }
  17. if (_fieldNames.TryGetValue(name, out int o))
  18. {
  19. return o;
  20. }
  21. return -1;
  22. }
  23.  
  24. public TValue TryGetValue<TValue>(string name)
  25. {
  26. int ordinal = IndexOf(name);
  27. if (ordinal != -1)
  28. {
  29. object o = GetValue(ordinal);
  30.  
  31. if (o != DBNull.Value) return (TValue)o;
  32. }
  33.  
  34. return default(TValue);
  35. }
  36.  
  37. private readonly IDataReader _reader;
  38. private Dictionary<string, int> _fieldNames = null;
  39.  
  40. public object this[int i] => _reader[i];
  41. public object this[string name] => _reader[name];
  42. public int FieldCount => _reader.FieldCount;
  43. public bool GetBoolean(int i) => _reader.GetBoolean(i);
  44. public byte GetByte(int i) => _reader.GetByte(i);
  45. public long GetBytes(int i, long fieldOffset, byte[] buffer, int bufferoffset, int length) => _reader.GetBytes(i, fieldOffset, buffer, bufferoffset, length);
  46. public char GetChar(int i) => _reader.GetChar(i);
  47. public long GetChars(int i, long fieldoffset, char[] buffer, int bufferoffset, int length) => _reader.GetChars(i, fieldoffset, buffer, bufferoffset, length);
  48. public IDataReader GetData(int i) => _reader.GetData(i);
  49. public string GetDataTypeName(int i) => _reader.GetDataTypeName(i);
  50. public DateTime GetDateTime(int i) => _reader.GetDateTime(i);
  51. public decimal GetDecimal(int i) => _reader.GetDecimal(i);
  52. public double GetDouble(int i) => _reader.GetDouble(i);
  53. public Type GetFieldType(int i) => _reader.GetFieldType(i);
  54. public float GetFloat(int i) => _reader.GetFloat(i);
  55. public Guid GetGuid(int i) => _reader.GetGuid(i);
  56. public short GetInt16(int i) => _reader.GetInt16(i);
  57. public int GetInt32(int i) => _reader.GetInt32(i);
  58. public long GetInt64(int i) => _reader.GetInt64(i);
  59. public string GetName(int i) => _reader.GetName(i);
  60. public int GetOrdinal(string name) => _reader.GetOrdinal(name);
  61. public string GetString(int i) => _reader.GetString(i);
  62. public object GetValue(int i) => _reader.GetValue(i);
  63. public int GetValues(object[] values) => _reader.GetValues(values);
  64. public bool IsDBNull(int i) => _reader.IsDBNull(i);
  65. }
Add Comment
Please, Sign In to add comment