DbEnumerator.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. //
  2. // System.Data.SqlClient.DbEnumerator.cs
  3. //
  4. // Author:
  5. // Tim Coleman ([email protected])
  6. //
  7. // Copyright (C) Tim Coleman, 2002
  8. //
  9. using System;
  10. using System.Collections;
  11. using System.Data;
  12. namespace System.Data.Common {
  13. public class DbEnumerator : IEnumerator
  14. {
  15. #region Fields
  16. IDataReader reader;
  17. bool closeReader;
  18. SchemaInfo[] schema;
  19. FieldNameLookup lookup;
  20. int fieldCount;
  21. #endregion // Fields
  22. #region Constructors
  23. public DbEnumerator (IDataReader reader)
  24. : this (reader, false)
  25. {
  26. }
  27. public DbEnumerator (IDataReader reader, bool closeReader)
  28. {
  29. this.reader = reader;
  30. this.closeReader = closeReader;
  31. this.lookup = new FieldNameLookup ();
  32. this.fieldCount = reader.FieldCount;
  33. LoadSchema (reader.GetSchemaTable ());
  34. }
  35. #endregion // Constructors
  36. #region Properties
  37. public virtual object Current {
  38. get {
  39. object[] values = new object[fieldCount];
  40. reader.GetValues (values);
  41. return new DbDataRecord (schema, values, lookup);
  42. }
  43. }
  44. #endregion // Properties
  45. #region Methods
  46. public void LoadSchema (DataTable schemaTable)
  47. {
  48. schema = new SchemaInfo [fieldCount];
  49. int index = 0;
  50. foreach (DataRow row in schemaTable.Rows) {
  51. SchemaInfo columnSchema = new SchemaInfo ();
  52. lookup.Add ((string) row["ColumnName"]);
  53. columnSchema.AllowDBNull = (bool) row ["AllowDBNull"];
  54. columnSchema.ColumnName = row ["ColumnName"].ToString ();
  55. columnSchema.ColumnOrdinal = (int) row ["ColumnOrdinal"];
  56. columnSchema.ColumnSize = (int) row ["ColumnSize"];
  57. columnSchema.DataTypeName = reader.GetDataTypeName (index);
  58. columnSchema.FieldType = reader.GetFieldType (index);
  59. columnSchema.IsReadOnly = (bool) row ["IsReadOnly"];
  60. columnSchema.TableName = row ["BaseTableName"].ToString ();
  61. if (row["NumericPrecision"] != DBNull.Value)
  62. columnSchema.NumericPrecision = (byte) row["NumericPrecision"];
  63. else
  64. columnSchema.NumericPrecision = (byte) 0;
  65. if (row["NumericScale"] != DBNull.Value)
  66. columnSchema.NumericScale = (byte) row["NumericScale"];
  67. else
  68. columnSchema.NumericScale = (byte) 0;
  69. schema[index] = columnSchema;
  70. index += 1;
  71. }
  72. }
  73. public virtual bool MoveNext ()
  74. {
  75. if (reader.Read ())
  76. return true;
  77. if (closeReader)
  78. reader.Close ();
  79. return false;
  80. }
  81. public virtual void Reset ()
  82. {
  83. throw new InvalidOperationException ("This enumerator can only go forward.");
  84. }
  85. #endregion // Methods
  86. }
  87. }