DataColumnMapping.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. //
  2. // System.Data.Common.DataColumnMapping.cs
  3. //
  4. // Authors:
  5. // Rodrigo Moya ([email protected])
  6. // Tim Coleman ([email protected])
  7. //
  8. // (C) Ximian, Inc
  9. // Copyright (C) Tim Coleman, 2002-2003
  10. //
  11. //
  12. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  13. //
  14. // Permission is hereby granted, free of charge, to any person obtaining
  15. // a copy of this software and associated documentation files (the
  16. // "Software"), to deal in the Software without restriction, including
  17. // without limitation the rights to use, copy, modify, merge, publish,
  18. // distribute, sublicense, and/or sell copies of the Software, and to
  19. // permit persons to whom the Software is furnished to do so, subject to
  20. // the following conditions:
  21. //
  22. // The above copyright notice and this permission notice shall be
  23. // included in all copies or substantial portions of the Software.
  24. //
  25. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  29. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  30. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  31. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  32. //
  33. using System.ComponentModel;
  34. using System.Data;
  35. namespace System.Data.Common {
  36. [TypeConverterAttribute (typeof (DataColumnMappingConverter))]
  37. public sealed class DataColumnMapping : MarshalByRefObject, IColumnMapping, ICloneable
  38. {
  39. #region Fields
  40. string sourceColumn;
  41. string dataSetColumn;
  42. #endregion // Fields
  43. #region Constructors
  44. public DataColumnMapping ()
  45. {
  46. sourceColumn = String.Empty;
  47. dataSetColumn = String.Empty;
  48. }
  49. public DataColumnMapping (string sourceColumn, string dataSetColumn)
  50. {
  51. this.sourceColumn = sourceColumn;
  52. this.dataSetColumn = dataSetColumn;
  53. }
  54. #endregion // Constructors
  55. #region Properties
  56. #if !NET_2_0
  57. [DataSysDescription ("DataColumn.ColumnName")]
  58. #endif
  59. [DefaultValue ("")]
  60. public string DataSetColumn {
  61. get { return dataSetColumn; }
  62. set { dataSetColumn = value; }
  63. }
  64. #if !NET_2_0
  65. [DataSysDescription ("Source column name - case sensitive.")]
  66. #endif
  67. [DefaultValue ("")]
  68. public string SourceColumn {
  69. get { return sourceColumn; }
  70. set { sourceColumn = value; }
  71. }
  72. #endregion // Properties
  73. #region Methods
  74. [EditorBrowsable (EditorBrowsableState.Advanced)]
  75. public DataColumn GetDataColumnBySchemaAction (DataTable dataTable, Type dataType, MissingSchemaAction schemaAction)
  76. {
  77. if (dataTable.Columns.Contains (dataSetColumn))
  78. return dataTable.Columns [dataSetColumn];
  79. if (schemaAction == MissingSchemaAction.Ignore)
  80. return null;
  81. if (schemaAction == MissingSchemaAction.Error)
  82. throw new InvalidOperationException (String.Format ("Missing the DataColumn '{0}' in the DataTable '{1}' for the SourceColumn '{2}'", DataSetColumn, dataTable.TableName, SourceColumn));
  83. return new DataColumn (dataSetColumn, dataType);
  84. }
  85. #if NET_2_0
  86. [EditorBrowsable (EditorBrowsableState.Advanced)]
  87. public static DataColumn GetDataColumnBySchemaAction (string sourceColumn, string dataSetColumn, DataTable dataTable, Type dataType, MissingSchemaAction schemaAction)
  88. {
  89. if (dataTable.Columns.Contains (dataSetColumn))
  90. return dataTable.Columns [dataSetColumn];
  91. if (schemaAction == MissingSchemaAction.Ignore)
  92. return null;
  93. if (schemaAction == MissingSchemaAction.Error)
  94. throw new InvalidOperationException (String.Format ("Missing the DataColumn '{0}' in the DataTable '{1}' for the SourceColumn '{2}'", dataSetColumn, dataTable.TableName, sourceColumn));
  95. return new DataColumn (dataSetColumn, dataType);
  96. }
  97. #endif
  98. object ICloneable.Clone ()
  99. {
  100. return new DataColumnMapping (SourceColumn, DataSetColumn);
  101. }
  102. public override string ToString ()
  103. {
  104. return SourceColumn;
  105. }
  106. #endregion // Methods
  107. }
  108. }