DbEnumerator.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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.ComponentModel;
  12. using System.Data;
  13. namespace System.Data.Common {
  14. public class DbEnumerator : IEnumerator
  15. {
  16. #region Fields
  17. IDataReader reader;
  18. bool closeReader;
  19. SchemaInfo[] schema;
  20. FieldNameLookup lookup;
  21. int fieldCount;
  22. #endregion // Fields
  23. #region Constructors
  24. public DbEnumerator (IDataReader reader)
  25. : this (reader, false)
  26. {
  27. }
  28. public DbEnumerator (IDataReader reader, bool closeReader)
  29. {
  30. this.reader = reader;
  31. this.closeReader = closeReader;
  32. this.lookup = new FieldNameLookup ();
  33. this.fieldCount = reader.FieldCount;
  34. LoadSchema (reader.GetSchemaTable ());
  35. }
  36. #endregion // Constructors
  37. #region Properties
  38. public object Current {
  39. get {
  40. object[] values = new object[fieldCount];
  41. reader.GetValues (values);
  42. return new DbDataRecord (schema, values, lookup);
  43. }
  44. }
  45. #endregion // Properties
  46. #region Methods
  47. private void LoadSchema (DataTable schemaTable)
  48. {
  49. schema = new SchemaInfo [fieldCount];
  50. int index = 0;
  51. foreach (DataRow row in schemaTable.Rows) {
  52. SchemaInfo columnSchema = new SchemaInfo ();
  53. lookup.Add ((string) row["ColumnName"]);
  54. columnSchema.AllowDBNull = (bool) row ["AllowDBNull"];
  55. columnSchema.ColumnName = row ["ColumnName"].ToString ();
  56. columnSchema.ColumnOrdinal = (int) row ["ColumnOrdinal"];
  57. columnSchema.ColumnSize = (int) row ["ColumnSize"];
  58. columnSchema.DataTypeName = reader.GetDataTypeName (index);
  59. columnSchema.FieldType = reader.GetFieldType (index);
  60. columnSchema.IsReadOnly = (bool) row ["IsReadOnly"];
  61. columnSchema.TableName = row ["BaseTableName"].ToString ();
  62. if (row ["NumericPrecision"] != DBNull.Value)
  63. columnSchema.NumericPrecision = Convert.ToByte (row ["NumericPrecision"]);
  64. else
  65. columnSchema.NumericPrecision = Convert.ToByte (0);
  66. if (row ["NumericScale"] != DBNull.Value)
  67. columnSchema.NumericScale = Convert.ToByte (row ["NumericScale"]);
  68. else
  69. columnSchema.NumericScale = Convert.ToByte (0);
  70. schema [index] = columnSchema;
  71. index += 1;
  72. }
  73. }
  74. public bool MoveNext ()
  75. {
  76. if (reader.Read ())
  77. return true;
  78. if (closeReader)
  79. reader.Close ();
  80. return false;
  81. }
  82. [EditorBrowsable (EditorBrowsableState.Never)]
  83. public void Reset ()
  84. {
  85. throw new NotSupportedException ();
  86. }
  87. #endregion // Methods
  88. }
  89. }