SqlDataAdapter.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. //
  2. // System.Data.SqlClient.SqlDataAdapter.cs
  3. //
  4. // Author:
  5. // Rodrigo Moya ([email protected])
  6. // Daniel Morgan ([email protected])
  7. // Tim Coleman ([email protected])
  8. //
  9. // (C) Ximian, Inc 2002
  10. // Copyright (C) 2002 Tim Coleman
  11. // (C) 2005 Mainsoft Corporation (http://www.mainsoft.com)
  12. //
  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;
  34. using System.ComponentModel;
  35. using System.Data;
  36. using System.Data.Common;
  37. namespace System.Data.SqlClient {
  38. [DefaultEvent ("RowUpdated")]
  39. public sealed class SqlDataAdapter : DbDataAdapter, IDbDataAdapter
  40. {
  41. #region Fields
  42. bool disposed = false;
  43. SqlCommand deleteCommand;
  44. SqlCommand insertCommand;
  45. SqlCommand selectCommand;
  46. SqlCommand updateCommand;
  47. #endregion
  48. #region Constructors
  49. public SqlDataAdapter ()
  50. : this (new SqlCommand ())
  51. {
  52. }
  53. public SqlDataAdapter (SqlCommand selectCommand)
  54. {
  55. DeleteCommand = null;
  56. InsertCommand = null;
  57. SelectCommand = selectCommand;
  58. UpdateCommand = null;
  59. }
  60. public SqlDataAdapter (string selectCommandText, SqlConnection selectConnection)
  61. : this (new SqlCommand (selectCommandText, selectConnection))
  62. {
  63. }
  64. public SqlDataAdapter (string selectCommandText, string selectConnectionString)
  65. : this (selectCommandText, new SqlConnection (selectConnectionString))
  66. {
  67. }
  68. #endregion
  69. #region Properties
  70. [DataCategory ("Update")]
  71. [DataSysDescription ("Used during Update for deleted rows in DataSet.")]
  72. [DefaultValue (null)]
  73. public SqlCommand DeleteCommand {
  74. get { return deleteCommand; }
  75. set { deleteCommand = value; }
  76. }
  77. [DataCategory ("Update")]
  78. [DataSysDescription ("Used during Update for new rows in DataSet.")]
  79. [DefaultValue (null)]
  80. public SqlCommand InsertCommand {
  81. get { return insertCommand; }
  82. set { insertCommand = value; }
  83. }
  84. [DataCategory ("Fill")]
  85. [DataSysDescription ("Used during Fill/FillSchema.")]
  86. [DefaultValue (null)]
  87. public SqlCommand SelectCommand {
  88. get { return selectCommand; }
  89. set { selectCommand = value; }
  90. }
  91. [DataCategory ("Update")]
  92. [DataSysDescription ("Used during Update for modified rows in DataSet.")]
  93. [DefaultValue (null)]
  94. public SqlCommand UpdateCommand {
  95. get { return updateCommand; }
  96. set { updateCommand = value; }
  97. }
  98. IDbCommand IDbDataAdapter.DeleteCommand {
  99. get { return DeleteCommand; }
  100. set {
  101. if (value != null && !(value is SqlCommand))
  102. throw new ArgumentException ("DeleteCommand is not of Type SqlCommand");
  103. DeleteCommand = (SqlCommand)value;
  104. }
  105. }
  106. IDbCommand IDbDataAdapter.InsertCommand {
  107. get { return InsertCommand; }
  108. set {
  109. if (value != null && !(value is SqlCommand))
  110. throw new ArgumentException ("InsertCommand is not of Type SqlCommand");
  111. InsertCommand = (SqlCommand)value;
  112. }
  113. }
  114. IDbCommand IDbDataAdapter.SelectCommand {
  115. get { return SelectCommand; }
  116. set {
  117. if (value != null && !(value is SqlCommand))
  118. throw new ArgumentException ("SelectCommand is not of Type SqlCommand");
  119. SelectCommand = (SqlCommand)value;
  120. }
  121. }
  122. IDbCommand IDbDataAdapter.UpdateCommand {
  123. get { return UpdateCommand; }
  124. set {
  125. if (value != null && !(value is SqlCommand))
  126. throw new ArgumentException ("UpdateCommand is not of Type SqlCommand");
  127. UpdateCommand = (SqlCommand)value;
  128. }
  129. }
  130. ITableMappingCollection IDataAdapter.TableMappings {
  131. get { return TableMappings; }
  132. }
  133. #endregion // Properties
  134. #region Methods
  135. protected override RowUpdatedEventArgs CreateRowUpdatedEvent (DataRow dataRow, IDbCommand command, StatementType statementType, DataTableMapping tableMapping)
  136. {
  137. return new SqlRowUpdatedEventArgs (dataRow, command, statementType, tableMapping);
  138. }
  139. protected override RowUpdatingEventArgs CreateRowUpdatingEvent (DataRow dataRow, IDbCommand command, StatementType statementType, DataTableMapping tableMapping)
  140. {
  141. return new SqlRowUpdatingEventArgs (dataRow, command, statementType, tableMapping);
  142. }
  143. protected override void Dispose (bool disposing)
  144. {
  145. if (!disposed) {
  146. if (disposing) {
  147. // Release managed resources
  148. }
  149. // Release unmanaged resources
  150. disposed = true;
  151. }
  152. }
  153. protected override void OnRowUpdated (RowUpdatedEventArgs value)
  154. {
  155. if (RowUpdated != null)
  156. RowUpdated (this, (SqlRowUpdatedEventArgs) value);
  157. }
  158. protected override void OnRowUpdating (RowUpdatingEventArgs value)
  159. {
  160. if (RowUpdating != null)
  161. RowUpdating (this, (SqlRowUpdatingEventArgs) value);
  162. }
  163. #endregion // Methods
  164. #region Events and Delegates
  165. [DataCategory ("Update")]
  166. [DataSysDescription ("Event triggered before every DataRow during Update.")]
  167. public event SqlRowUpdatedEventHandler RowUpdated;
  168. [DataCategory ("Update")]
  169. [DataSysDescription ("Event triggered after every DataRow during Update.")]
  170. public event SqlRowUpdatingEventHandler RowUpdating;
  171. #endregion // Events and Delegates
  172. }
  173. }