DataColumnMapping.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. [DataSysDescription ("DataColumn.ColumnName")]
  57. [DefaultValue ("")]
  58. public string DataSetColumn {
  59. get { return dataSetColumn; }
  60. set { dataSetColumn = value; }
  61. }
  62. [DataSysDescription ("Source column name - case sensitive.")]
  63. [DefaultValue ("")]
  64. public string SourceColumn {
  65. get { return sourceColumn; }
  66. set { sourceColumn = value; }
  67. }
  68. #endregion // Properties
  69. #region Methods
  70. [EditorBrowsable (EditorBrowsableState.Advanced)]
  71. public DataColumn GetDataColumnBySchemaAction (DataTable dataTable, Type dataType, MissingSchemaAction schemaAction)
  72. {
  73. if (dataTable.Columns.Contains (dataSetColumn))
  74. return dataTable.Columns [dataSetColumn];
  75. if (schemaAction == MissingSchemaAction.Ignore)
  76. return null;
  77. if (schemaAction == MissingSchemaAction.Error)
  78. throw new InvalidOperationException (String.Format ("Missing the DataColumn '{0}' in the DataTable '{1}' for the SourceColumn '{2}'", DataSetColumn, dataTable.TableName, SourceColumn));
  79. return new DataColumn (dataSetColumn, dataType);
  80. }
  81. #if NET_2_0
  82. [MonoTODO]
  83. public static DataColumn GetDataColumnBySchemaAction (string sourceColumn, string dataSetColumn, DataTable dataTable, Type dataType, MissingSchemaAction schemaAction)
  84. {
  85. throw new NotImplementedException ();
  86. }
  87. #endif
  88. object ICloneable.Clone ()
  89. {
  90. return new DataColumnMapping (SourceColumn, DataSetColumn);
  91. }
  92. public override string ToString ()
  93. {
  94. return SourceColumn;
  95. }
  96. #endregion // Methods
  97. }
  98. }