SqlDataAdapter.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. //
  12. //
  13. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  14. //
  15. // Permission is hereby granted, free of charge, to any person obtaining
  16. // a copy of this software and associated documentation files (the
  17. // "Software"), to deal in the Software without restriction, including
  18. // without limitation the rights to use, copy, modify, merge, publish,
  19. // distribute, sublicense, and/or sell copies of the Software, and to
  20. // permit persons to whom the Software is furnished to do so, subject to
  21. // the following conditions:
  22. //
  23. // The above copyright notice and this permission notice shall be
  24. // included in all copies or substantial portions of the Software.
  25. //
  26. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  27. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  28. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  29. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  30. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  31. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  32. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  33. //
  34. using System;
  35. using System.ComponentModel;
  36. using System.Data;
  37. using System.Data.Common;
  38. namespace System.Data.SqlClient {
  39. [DefaultEvent ("RowUpdated")]
  40. [DesignerAttribute ("Microsoft.VSDesigner.Data.VS.SqlDataAdapterDesigner, "+ Consts.AssemblyMicrosoft_VSDesigner, "System.ComponentModel.Design.IDesigner")]
  41. [ToolboxItemAttribute ("Microsoft.VSDesigner.Data.VS.SqlDataAdapterToolboxItem, "+ Consts.AssemblyMicrosoft_VSDesigner)]
  42. public sealed class SqlDataAdapter : DbDataAdapter, IDbDataAdapter
  43. {
  44. #region Fields
  45. bool disposed = false;
  46. SqlCommand deleteCommand;
  47. SqlCommand insertCommand;
  48. SqlCommand selectCommand;
  49. SqlCommand updateCommand;
  50. #if NET_2_0
  51. int updateBatchSize;
  52. #endif
  53. #endregion
  54. #region Constructors
  55. public SqlDataAdapter ()
  56. : this (new SqlCommand ())
  57. {
  58. }
  59. public SqlDataAdapter (SqlCommand selectCommand)
  60. {
  61. DeleteCommand = null;
  62. InsertCommand = null;
  63. SelectCommand = selectCommand;
  64. UpdateCommand = null;
  65. #if NET_2_0
  66. UpdateBatchSize = 1;
  67. #endif
  68. }
  69. public SqlDataAdapter (string selectCommandText, SqlConnection selectConnection)
  70. : this (new SqlCommand (selectCommandText, selectConnection))
  71. {
  72. }
  73. public SqlDataAdapter (string selectCommandText, string selectConnectionString)
  74. : this (selectCommandText, new SqlConnection (selectConnectionString))
  75. {
  76. }
  77. #endregion
  78. #region Properties
  79. [DataCategory ("Update")]
  80. #if !NET_2_0
  81. [DataSysDescription ("Used during Update for deleted rows in DataSet.")]
  82. #endif
  83. [DefaultValue (null)]
  84. [EditorAttribute ("Microsoft.VSDesigner.Data.Design.DBCommandEditor, "+ Consts.AssemblyMicrosoft_VSDesigner, "System.Drawing.Design.UITypeEditor, "+ Consts.AssemblySystem_Drawing )]
  85. public new SqlCommand DeleteCommand {
  86. get { return deleteCommand; }
  87. set { deleteCommand = value; }
  88. }
  89. [DataCategory ("Update")]
  90. #if !NET_2_0
  91. [DataSysDescription ("Used during Update for new rows in DataSet.")]
  92. #endif
  93. [DefaultValue (null)]
  94. [EditorAttribute ("Microsoft.VSDesigner.Data.Design.DBCommandEditor, "+ Consts.AssemblyMicrosoft_VSDesigner, "System.Drawing.Design.UITypeEditor, "+ Consts.AssemblySystem_Drawing )]
  95. public new SqlCommand InsertCommand {
  96. get { return insertCommand; }
  97. set { insertCommand = value; }
  98. }
  99. [DataCategory ("Fill")]
  100. #if !NET_2_0
  101. [DataSysDescription ("Used during Fill/FillSchema.")]
  102. #endif
  103. [DefaultValue (null)]
  104. [EditorAttribute ("Microsoft.VSDesigner.Data.Design.DBCommandEditor, "+ Consts.AssemblyMicrosoft_VSDesigner, "System.Drawing.Design.UITypeEditor, "+ Consts.AssemblySystem_Drawing )]
  105. public new SqlCommand SelectCommand {
  106. get { return selectCommand; }
  107. set { selectCommand = value; }
  108. }
  109. [DataCategory ("Update")]
  110. #if !NET_2_0
  111. [DataSysDescription ("Used during Update for modified rows in DataSet.")]
  112. #endif
  113. [DefaultValue (null)]
  114. [EditorAttribute ("Microsoft.VSDesigner.Data.Design.DBCommandEditor, "+ Consts.AssemblyMicrosoft_VSDesigner, "System.Drawing.Design.UITypeEditor, "+ Consts.AssemblySystem_Drawing )]
  115. public new SqlCommand UpdateCommand {
  116. get { return updateCommand; }
  117. set { updateCommand = value; }
  118. }
  119. IDbCommand IDbDataAdapter.DeleteCommand {
  120. get { return DeleteCommand; }
  121. set {
  122. if (!(value is SqlCommand))
  123. throw new ArgumentException ();
  124. DeleteCommand = (SqlCommand)value;
  125. }
  126. }
  127. IDbCommand IDbDataAdapter.InsertCommand {
  128. get { return InsertCommand; }
  129. set {
  130. if (!(value is SqlCommand))
  131. throw new ArgumentException ();
  132. InsertCommand = (SqlCommand)value;
  133. }
  134. }
  135. IDbCommand IDbDataAdapter.SelectCommand {
  136. get { return SelectCommand; }
  137. set {
  138. if (!(value is SqlCommand))
  139. throw new ArgumentException ();
  140. SelectCommand = (SqlCommand)value;
  141. }
  142. }
  143. IDbCommand IDbDataAdapter.UpdateCommand {
  144. get { return UpdateCommand; }
  145. set {
  146. if (!(value is SqlCommand))
  147. throw new ArgumentException ();
  148. UpdateCommand = (SqlCommand)value;
  149. }
  150. }
  151. ITableMappingCollection IDataAdapter.TableMappings {
  152. get { return TableMappings; }
  153. }
  154. #if NET_2_0
  155. public override int UpdateBatchSize {
  156. get { return updateBatchSize; }
  157. set {
  158. if (value < 0)
  159. throw new ArgumentOutOfRangeException ();
  160. updateBatchSize = value;
  161. }
  162. }
  163. #endif
  164. #endregion // Properties
  165. #region Methods
  166. protected override RowUpdatedEventArgs CreateRowUpdatedEvent (DataRow dataRow, IDbCommand command, StatementType statementType, DataTableMapping tableMapping)
  167. {
  168. return new SqlRowUpdatedEventArgs (dataRow, command, statementType, tableMapping);
  169. }
  170. protected override RowUpdatingEventArgs CreateRowUpdatingEvent (DataRow dataRow, IDbCommand command, StatementType statementType, DataTableMapping tableMapping)
  171. {
  172. return new SqlRowUpdatingEventArgs (dataRow, command, statementType, tableMapping);
  173. }
  174. protected override void Dispose (bool disposing)
  175. {
  176. if (!disposed) {
  177. if (disposing) {
  178. // Release managed resources
  179. }
  180. // Release unmanaged resources
  181. disposed = true;
  182. }
  183. }
  184. protected override void OnRowUpdated (RowUpdatedEventArgs value)
  185. {
  186. if (RowUpdated != null)
  187. RowUpdated (this, (SqlRowUpdatedEventArgs) value);
  188. }
  189. protected override void OnRowUpdating (RowUpdatingEventArgs value)
  190. {
  191. if (RowUpdating != null)
  192. RowUpdating (this, (SqlRowUpdatingEventArgs) value);
  193. }
  194. #endregion // Methods
  195. #region Events and Delegates
  196. [DataCategory ("Update")]
  197. #if !NET_2_0
  198. [DataSysDescription ("Event triggered before every DataRow during Update.")]
  199. #endif
  200. public event SqlRowUpdatedEventHandler RowUpdated;
  201. [DataCategory ("Update")]
  202. #if !NET_2_0
  203. [DataSysDescription ("Event triggered after every DataRow during Update.")]
  204. #endif
  205. public event SqlRowUpdatingEventHandler RowUpdating;
  206. #endregion // Events and Delegates
  207. }
  208. }