SqlDataAdapter.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. [DefaultEvent ("RowUpdated")]
  18. public sealed class SqlDataAdapter : DbDataAdapter, IDbDataAdapter
  19. {
  20. #region Fields
  21. SqlCommand deleteCommand;
  22. SqlCommand insertCommand;
  23. SqlCommand selectCommand;
  24. SqlCommand updateCommand;
  25. static readonly object EventRowUpdated = new object();
  26. static readonly object EventRowUpdating = new object();
  27. #endregion
  28. #region Constructors
  29. public SqlDataAdapter ()
  30. : this (new SqlCommand ())
  31. {
  32. }
  33. public SqlDataAdapter (SqlCommand selectCommand)
  34. {
  35. DeleteCommand = new SqlCommand ();
  36. InsertCommand = new SqlCommand ();
  37. SelectCommand = selectCommand;
  38. UpdateCommand = new SqlCommand ();
  39. }
  40. public SqlDataAdapter (string selectCommandText, SqlConnection selectConnection)
  41. : this (new SqlCommand (selectCommandText, selectConnection))
  42. {
  43. }
  44. public SqlDataAdapter (string selectCommandText, string selectConnectionString)
  45. : this (selectCommandText, new SqlConnection (selectConnectionString))
  46. {
  47. }
  48. #endregion
  49. #region Properties
  50. [DataSysDescription ("Used during Update for deleted rows in DataSet.")]
  51. [DefaultValue (null)]
  52. public SqlCommand DeleteCommand {
  53. get { return deleteCommand; }
  54. set { deleteCommand = value; }
  55. }
  56. [DataSysDescription ("Used during Update for new rows in DataSet.")]
  57. [DefaultValue (null)]
  58. public SqlCommand InsertCommand {
  59. get { return insertCommand; }
  60. set { insertCommand = value; }
  61. }
  62. [DataSysDescription ("Used during Fill/FillSchema.")]
  63. [DefaultValue (null)]
  64. public SqlCommand SelectCommand {
  65. get { return selectCommand; }
  66. set { selectCommand = value; }
  67. }
  68. [DataSysDescription ("Used during Update for modified rows in DataSet.")]
  69. [DefaultValue (null)]
  70. public SqlCommand UpdateCommand {
  71. get { return updateCommand; }
  72. set { updateCommand = value; }
  73. }
  74. IDbCommand IDbDataAdapter.DeleteCommand {
  75. get { return DeleteCommand; }
  76. set {
  77. if (!(value is SqlCommand))
  78. throw new ArgumentException ();
  79. DeleteCommand = (SqlCommand)value;
  80. }
  81. }
  82. IDbCommand IDbDataAdapter.InsertCommand {
  83. get { return InsertCommand; }
  84. set {
  85. if (!(value is SqlCommand))
  86. throw new ArgumentException ();
  87. InsertCommand = (SqlCommand)value;
  88. }
  89. }
  90. IDbCommand IDbDataAdapter.SelectCommand {
  91. get { return SelectCommand; }
  92. set {
  93. if (!(value is SqlCommand))
  94. throw new ArgumentException ();
  95. SelectCommand = (SqlCommand)value;
  96. }
  97. }
  98. IDbCommand IDbDataAdapter.UpdateCommand {
  99. get { return UpdateCommand; }
  100. set {
  101. if (!(value is SqlCommand))
  102. throw new ArgumentException ();
  103. UpdateCommand = (SqlCommand)value;
  104. }
  105. }
  106. ITableMappingCollection IDataAdapter.TableMappings {
  107. get { return TableMappings; }
  108. }
  109. #endregion // Properties
  110. #region Methods
  111. protected override RowUpdatedEventArgs CreateRowUpdatedEvent (DataRow dataRow, IDbCommand command, StatementType statementType, DataTableMapping tableMapping)
  112. {
  113. return new SqlRowUpdatedEventArgs (dataRow, command, statementType, tableMapping);
  114. }
  115. protected override RowUpdatingEventArgs CreateRowUpdatingEvent (DataRow dataRow, IDbCommand command, StatementType statementType, DataTableMapping tableMapping)
  116. {
  117. return new SqlRowUpdatingEventArgs (dataRow, command, statementType, tableMapping);
  118. }
  119. protected override void OnRowUpdated (RowUpdatedEventArgs value)
  120. {
  121. SqlRowUpdatedEventHandler handler = (SqlRowUpdatedEventHandler) Events[EventRowUpdated];
  122. if ((handler != null) && (value is SqlRowUpdatedEventArgs))
  123. handler(this, (SqlRowUpdatedEventArgs) value);
  124. }
  125. protected override void OnRowUpdating (RowUpdatingEventArgs value)
  126. {
  127. SqlRowUpdatingEventHandler handler = (SqlRowUpdatingEventHandler) Events[EventRowUpdating];
  128. if ((handler != null) && (value is SqlRowUpdatingEventArgs))
  129. handler(this, (SqlRowUpdatingEventArgs) value);
  130. }
  131. #endregion // Methods
  132. #region Events and Delegates
  133. [DataSysDescription ("Event triggered before every DataRow during Update.")]
  134. public event SqlRowUpdatedEventHandler RowUpdated {
  135. add { Events.AddHandler (EventRowUpdated, value); }
  136. remove { Events.RemoveHandler (EventRowUpdated, value); }
  137. }
  138. [DataSysDescription ("Event triggered after every DataRow during Update.")]
  139. public event SqlRowUpdatingEventHandler RowUpdating {
  140. add { Events.AddHandler (EventRowUpdating, value); }
  141. remove { Events.RemoveHandler (EventRowUpdating, value); }
  142. }
  143. #endregion // Events and Delegates
  144. }
  145. }