SqlDataAdapter.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. using System;
  13. using System.ComponentModel;
  14. using System.Data;
  15. using System.Data.Common;
  16. namespace System.Data.SqlClient
  17. {
  18. /// <summary>
  19. /// Represents a set of command-related properties that are used
  20. /// to fill the DataSet and update a data source, all this
  21. /// from a SQL database.
  22. /// </summary>
  23. public sealed class SqlDataAdapter : DbDataAdapter, IDbDataAdapter
  24. {
  25. #region Fields
  26. SqlCommand deleteCommand;
  27. SqlCommand insertCommand;
  28. SqlCommand selectCommand;
  29. SqlCommand updateCommand;
  30. static readonly object EventRowUpdated = new object();
  31. static readonly object EventRowUpdating = new object();
  32. #endregion
  33. #region Constructors
  34. public SqlDataAdapter ()
  35. : this (new SqlCommand ())
  36. {
  37. }
  38. public SqlDataAdapter (SqlCommand selectCommand)
  39. {
  40. DeleteCommand = new SqlCommand ();
  41. InsertCommand = new SqlCommand ();
  42. SelectCommand = selectCommand;
  43. UpdateCommand = new SqlCommand ();
  44. }
  45. public SqlDataAdapter (string selectCommandText, SqlConnection selectConnection)
  46. : this (new SqlCommand (selectCommandText, selectConnection))
  47. {
  48. }
  49. public SqlDataAdapter (string selectCommandText, string selectConnectionString)
  50. : this (selectCommandText, new SqlConnection (selectConnectionString))
  51. {
  52. }
  53. #endregion
  54. #region Properties
  55. public SqlCommand DeleteCommand {
  56. get {
  57. return deleteCommand;
  58. }
  59. set {
  60. deleteCommand = value;
  61. }
  62. }
  63. public SqlCommand InsertCommand {
  64. get {
  65. return insertCommand;
  66. }
  67. set {
  68. insertCommand = value;
  69. }
  70. }
  71. public SqlCommand SelectCommand {
  72. get {
  73. return selectCommand;
  74. }
  75. set {
  76. selectCommand = value;
  77. }
  78. }
  79. public SqlCommand UpdateCommand {
  80. get {
  81. return updateCommand;
  82. }
  83. set {
  84. updateCommand = value;
  85. }
  86. }
  87. IDbCommand IDbDataAdapter.DeleteCommand {
  88. get { return DeleteCommand; }
  89. set {
  90. if (!(value is SqlCommand))
  91. throw new ArgumentException ();
  92. DeleteCommand = (SqlCommand)value;
  93. }
  94. }
  95. IDbCommand IDbDataAdapter.InsertCommand {
  96. get { return InsertCommand; }
  97. set {
  98. if (!(value is SqlCommand))
  99. throw new ArgumentException ();
  100. InsertCommand = (SqlCommand)value;
  101. }
  102. }
  103. IDbCommand IDbDataAdapter.SelectCommand {
  104. get { return SelectCommand; }
  105. set {
  106. if (!(value is SqlCommand))
  107. throw new ArgumentException ();
  108. SelectCommand = (SqlCommand)value;
  109. }
  110. }
  111. IDbCommand IDbDataAdapter.UpdateCommand {
  112. get { return UpdateCommand; }
  113. set {
  114. if (!(value is SqlCommand))
  115. throw new ArgumentException ();
  116. UpdateCommand = (SqlCommand)value;
  117. }
  118. }
  119. ITableMappingCollection IDataAdapter.TableMappings {
  120. get { return TableMappings; }
  121. }
  122. #endregion // Properties
  123. #region Methods
  124. protected override RowUpdatedEventArgs CreateRowUpdatedEvent (DataRow dataRow, IDbCommand command, StatementType statementType, DataTableMapping tableMapping)
  125. {
  126. return new SqlRowUpdatedEventArgs (dataRow, command, statementType, tableMapping);
  127. }
  128. protected override RowUpdatingEventArgs CreateRowUpdatingEvent (DataRow dataRow, IDbCommand command, StatementType statementType, DataTableMapping tableMapping)
  129. {
  130. return new SqlRowUpdatingEventArgs (dataRow, command, statementType, tableMapping);
  131. }
  132. protected override void OnRowUpdated (RowUpdatedEventArgs value)
  133. {
  134. SqlRowUpdatedEventHandler handler = (SqlRowUpdatedEventHandler) Events[EventRowUpdated];
  135. if ((handler != null) && (value is SqlRowUpdatedEventArgs))
  136. handler(this, (SqlRowUpdatedEventArgs) value);
  137. }
  138. protected override void OnRowUpdating (RowUpdatingEventArgs value)
  139. {
  140. SqlRowUpdatingEventHandler handler = (SqlRowUpdatingEventHandler) Events[EventRowUpdating];
  141. if ((handler != null) && (value is SqlRowUpdatingEventArgs))
  142. handler(this, (SqlRowUpdatingEventArgs) value);
  143. }
  144. #endregion // Methods
  145. #region Events and Delegates
  146. public event SqlRowUpdatedEventHandler RowUpdated {
  147. add { Events.AddHandler (EventRowUpdated, value); }
  148. remove { Events.RemoveHandler (EventRowUpdated, value); }
  149. }
  150. public event SqlRowUpdatingEventHandler RowUpdating {
  151. add { Events.AddHandler (EventRowUpdating, value); }
  152. remove { Events.RemoveHandler (EventRowUpdating, value); }
  153. }
  154. #endregion // Events and Delegates
  155. }
  156. }