DataTableMapping.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. //
  2. // System.Data.Common.DataTableMapping.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
  10. //
  11. using System.ComponentModel;
  12. using System.Data;
  13. namespace System.Data.Common {
  14. public sealed class DataTableMapping : MarshalByRefObject, ITableMapping, ICloneable
  15. {
  16. #region Fields
  17. string sourceTable;
  18. string dataSetTable;
  19. DataColumnMappingCollection columnMappings;
  20. #endregion // Fields
  21. #region Constructors
  22. public DataTableMapping ()
  23. {
  24. dataSetTable = String.Empty;
  25. sourceTable = String.Empty;
  26. columnMappings = new DataColumnMappingCollection ();
  27. }
  28. public DataTableMapping (string sourceTable, string dataSetTable)
  29. : this ()
  30. {
  31. this.sourceTable = sourceTable;
  32. this.dataSetTable = dataSetTable;
  33. }
  34. public DataTableMapping (string sourceTable, string dataSetTable, DataColumnMapping[] columnMappings)
  35. : this (sourceTable, dataSetTable)
  36. {
  37. this.columnMappings.AddRange (columnMappings);
  38. }
  39. #endregion // Constructors
  40. #region Properties
  41. [DataSysDescription ("Individual columns mappings when this table mapping is matched.")]
  42. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  43. public DataColumnMappingCollection ColumnMappings {
  44. get { return columnMappings; }
  45. }
  46. [DataSysDescription ("DataTable.TableName")]
  47. [DefaultValue ("")]
  48. public string DataSetTable {
  49. get { return dataSetTable; }
  50. set { dataSetTable = value; }
  51. }
  52. IColumnMappingCollection ITableMapping.ColumnMappings {
  53. get { return ColumnMappings; }
  54. }
  55. [DataSysDescription ("The DataTableMapping source table name. This name is case sensitive.")]
  56. [DefaultValue ("")]
  57. public string SourceTable {
  58. get { return sourceTable; }
  59. set { sourceTable = value; }
  60. }
  61. #endregion // Properties
  62. #region Methods
  63. [EditorBrowsable (EditorBrowsableState.Advanced)]
  64. public DataColumnMapping GetColumnMappingBySchemaAction (string sourceColumn, MissingMappingAction mappingAction)
  65. {
  66. return DataColumnMappingCollection.GetColumnMappingBySchemaAction (columnMappings, sourceColumn, mappingAction);
  67. }
  68. [EditorBrowsable (EditorBrowsableState.Advanced)]
  69. public DataTable GetDataTableBySchemaAction (DataSet dataSet, MissingSchemaAction schemaAction)
  70. {
  71. if (dataSet.Tables.Contains (DataSetTable))
  72. return dataSet.Tables [DataSetTable];
  73. if (schemaAction == MissingSchemaAction.Ignore)
  74. return null;
  75. if (schemaAction == MissingSchemaAction.Error)
  76. throw new InvalidOperationException (String.Format ("Missing the '{0} DataTable for the '{1}' SourceTable", DataSetTable, SourceTable));
  77. return new DataTable (DataSetTable);
  78. }
  79. object ICloneable.Clone ()
  80. {
  81. return new DataTableMapping (SourceTable, DataSetTable);
  82. }
  83. public override string ToString ()
  84. {
  85. return SourceTable;
  86. }
  87. #endregion // Methods
  88. }
  89. }